Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSDoc with TS - show return interface comments in intellisense?

Let's say I have a function whose return value is a defined typescript interface. The interface is well annotated:

interface ReturnValue {
  /**
   * Wind value, in knots
   */
  wind: number;
  /**
   * Humidity in %
   */
  humidity: number;
}

/**
 * Function to return some values
 * @returns Returner
 */
const myFunction = (): ReturnValue => {
  return {
    wind: 4,
    humidity: 5
  };
};

I'd like to be able to see the annotations of the return value interface when I use intellisense, or when hovering over the function. Instead, this is what I get:

enter image description here

I get a little more detail when I don't name the interface but declare it directly as the return value of the function:

/**
 * Function to return some values
 */
const myFunction = (): {
  /**
   * Wind value, in knots
   */
  wind: number;
  /**
   * Humidity in %
   */
  humidity: number;
} => {
  return {
    wind: 4,
    humidity: 5
  };
};

This gives:

enter image description here

But even this doesn't the show comments I used when defining the return value object shape. Also its messy.

Is it possible to write JSDoc comments such that I can see them as part of the intellisense when defining return values in typescript?

like image 469
Seth Lutske Avatar asked Jan 22 '26 19:01

Seth Lutske


1 Answers

Sadly, you don't really have any control here.

Typescript types can get deep, and completely unfolding all of them always to their atomic pieces isn't really practical for more complex types. But, as you note, for simple types it can be a bit too quick to wrap them up in a named alias. A tooltip that has many pages of interface details to scroll through is almost as useless as very little information.

So typescript errs on the side of conciseness, opting to show you the types you have named rather than large granular interfaces.

If you want to know what in a type like this, you can just make one, type a . and then explore its interface:

enter image description here

And the JS doc comments do make their way through when you actually use them:

enter image description here

Though I agree it can be hard to understand what's in a type sometimes, I don't believe there are any ways to manipulate how TypeScript/VSCode unravel and report on your type aliases.

like image 97
Alex Wayne Avatar answered Jan 25 '26 11:01

Alex Wayne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!