javascript - How to get IDE intellisense autocompletion through JSDoc type hints with TypeScript packages? - Stack Overflow

I just noticed that we can get IDE intellisense autocompletion through JSDoc type hints like this (from

I just noticed that we can get IDE intellisense autocompletion through JSDoc type hints like this (from Vite config documentation):

/** @type {import('vite').UserConfig} */
export default {
  // ...
}

Is the import('vite') type functionality part of JSDoc? I've never seen this used before, so just curious how it works.

Can we get autocompletion for any type in a plain JavaScript project by using @type annotations like this?

I just noticed that we can get IDE intellisense autocompletion through JSDoc type hints like this (from Vite config documentation):

/** @type {import('vite').UserConfig} */
export default {
  // ...
}

Is the import('vite') type functionality part of JSDoc? I've never seen this used before, so just curious how it works.

Can we get autocompletion for any type in a plain JavaScript project by using @type annotations like this?

Share Improve this question edited Mar 25 at 8:54 jonrsharpe 122k30 gold badges268 silver badges475 bronze badges asked Mar 25 at 4:23 OleOle 47.5k70 gold badges237 silver badges445 bronze badges 2
  • 1 I don't see it from jsdoc's website: jsdoc.app/tags-type – xuhdev Commented Mar 25 at 7:50
  • 1 typescriptlang./docs/handbook/… – jonrsharpe Commented Mar 25 at 8:54
Add a comment  | 

2 Answers 2

Reset to default 1

All type hint are supplied by TypeScript's language server, enabled by default in VSCode & widely available in other editors.

JSDoc is just a standardized syntax for writing documentation in JS, functionally, it does nothing. The TS language server, however, has implemented a subset of the JSDoc syntax which will allow you to type your JS code without authoring TS (.ts). This will match TypeScript behavior in most cases, though there's a few odd cases where JSDoc trails behind or lacks support for a few features.

As linked above (https://www.typescriptlang./docs/handbook/jsdoc-supported-types.html), yes, you can use type comments to get autocompletion hints in plain JS using this:

/** @type {'foo' | 'bar'} */
const myString = 'baz';
   // ^ Type '"baz"' is not assignable to type '"foo" | "bar"'

The import(module) bit allows you to access types, as of course you cannot otherwise import types into JS modules.

As for "Can we get autocompletion in a plain JavaScript project by using JSDoc @type annotations from external TS type declarations [in VSCode]?":

Yes, definitely: @import or Triple slash directives inside "@ts-checked" work in VSC.

TS is "just" a superset of JS, and since most TS type-related features can also be equivalently expressed in JSDoc, the language server that VSCode uses basically makes no difference between them and treats JS files as TS when type checking them and providing the IntelliSense. (You can grab types for existing popular projects that lacks types from DefinitelyTyped project.)

As for "Are @importing type definitions from .d.ts a standard JSDoc feature?" question that may stem from the first one:

No, at least not de iure. Officially JSDoc still does not acknowledge TS. These all JSDoc-to-TS bindings seems to be de-facto standard leveraged in VSCode and its tooling.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744218053a4563646.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信