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?
- 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
2 Answers
Reset to default 1All 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-check
ed" 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 @import
ing 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条)