-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement a JSDoc @import tag #22160
Comments
This was previously discussed in #14377 |
closing in favor of #14844 |
@DanielRosenwasser Did the syntax for module namespaces change? I tried scanning through the linked issues but since none of them are showing the JSDoc equivalent, it's somewhat hard to follow. /**
* The following works using latest typescript@next (2.9.0-dev.20180506):
*
* @typedef {import('http').IncomingMessage} IncomingMessage
* @typedef {import('http').ServerResponse} ServerResponse
*
* But this fails:
*
* > Module 'http' does not refer to a type, but is used as a type here.
*
* @typedef {import('http')} http
*/ |
Use /** @typedef {typeof import('http')} http*/ |
Hi Typescript Magic Elves, Do you think you'll be able to get any decisions on what the final DRY-er jsdoc import syntax will look like? Just curious what the blocking items are? |
The original motivation to not do this was to just re-use as much well-understood JSDoc as we could. If we can get more feedback, I think it will be a strong enough signal that there's interest and that we should actually invest in the feature. |
I hope this doesn’t get stuck in analysis paralysis fever like some of the other issues. I am Typescript js-doccing a medium sized project and this is a pretty painful experience. It feels like a very subpar experience to TS. |
Kind ping to typescript authors and @DanielRosenwasser . Has any decision been regarding this? Using tsc as a typechecker for js with a verbose syntax like this adds up quite a bit of friction. |
Also, #22160 (comment) doesn’t support optional type parameters. |
Any updates on this? I'm still importing things at the top of the file, but CRA of course complains in the preflight. |
I've resorted to adding |
@jeffersoneagley I had great success with approach #1, and I found a way to make it interoperable with normal .js/.ts files inside ESM modules. This approach takes advantage of how TypeScript appears to merge file extensions if the filenames are the same. my-module.ts export type FooString = 'foo' | 'bar' | 'baz';
export type FooObject = { foo: FooString; }; my-module.js /**
* @file Dummy export in order to avoid a runtime ERR_MODULE_NOT_FOUND error.
*/
export {}; index.js import * as MyModule from './my-module.js';
/**
* @param {MyModule.FooString} fooString
* @return {MyModule.FooObject}
*/
export const createFooObject = (fooString) => {
return { foo: fooString };
}; Now it seems a bit silly to have to create a dummy export file every time, but in my case, I need to define and import 100+ namespaced types, and I really haven't found any other reasonable way to do it. |
Hope to use JSDoc syntax to import typescript definition to *.js file. |
@RobinBlomberg I actually define mine in types.d.ts files around the app now and they basically give every js file in the directory the ability to access types defined in pure TS (don't use any import statements other than inline though, top-of-file imports will turn the type file into a module and then you can't easily import it into js) |
@jeffersoneagley Genius! I'll have to try this. |
The Viewer API definitions do not compile because of missing imports and anonymous objects are typed as `Object`. These issues were not caught during CI because the test project was not compiling anything from the Viewer API. As an example of the first problem: ``` /** * @implements MyInterface */ export class MyClass { ... } ``` will generate a broken definition that doesn’t import MyInterface: ``` /** * @implements MyInterface */ export class MyClass implements MyInterface { ... } ``` This can be fixed by adding a typedef jsdoc to specify the import: ``` /** @typedef {import("./otherFile").MyInterface} MyInterface */ ``` See jsdoc/jsdoc#1537 and microsoft/TypeScript#22160 for more details. As an example of the second problem: ``` /** * Gets the size of the specified page, converted from PDF units to inches. * @param {Object} An Object containing the properties: {Array} `view`, * {number} `userUnit`, and {number} `rotate`. */ function getPageSizeInches({ view, userUnit, rotate }) { ... } ``` generates the broken definition: ``` function getPageSizeInches({ view, userUnit, rotate }: Object) { ... } ``` The jsdoc should specify the type of each nested property: ``` /** * Gets the size of the specified page, converted from PDF units to inches. * @param {Object} options An object containing the properties: {Array} `view`, * {number} `userUnit`, and {number} `rotate`. * @param {number[]} options.view * @param {number} options.userUnit * @param {number} options.rotate */ ```
It seems that this issue will never reach a proper conclusion, but this is blocking #46011.
|
The Viewer API definitions do not compile because of missing imports and anonymous objects are typed as `Object`. These issues were not caught during CI because the test project was not compiling anything from the Viewer API. As an example of the first problem: ``` /** * @implements MyInterface */ export class MyClass { ... } ``` will generate a broken definition that doesn’t import MyInterface: ``` /** * @implements MyInterface */ export class MyClass implements MyInterface { ... } ``` This can be fixed by adding a typedef jsdoc to specify the import: ``` /** @typedef {import("./otherFile").MyInterface} MyInterface */ ``` See jsdoc/jsdoc#1537 and microsoft/TypeScript#22160 for more details. As an example of the second problem: ``` /** * Gets the size of the specified page, converted from PDF units to inches. * @param {Object} An Object containing the properties: {Array} `view`, * {number} `userUnit`, and {number} `rotate`. */ function getPageSizeInches({ view, userUnit, rotate }) { ... } ``` generates the broken definition: ``` function getPageSizeInches({ view, userUnit, rotate }: Object) { ... } ``` The jsdoc should specify the type of each nested property: ``` /** * Gets the size of the specified page, converted from PDF units to inches. * @param {Object} options An object containing the properties: {Array} `view`, * {number} `userUnit`, and {number} `rotate`. * @param {number[]} options.view * @param {number} options.userUnit * @param {number} options.rotate */ ```
Thanks to @a-tarasyuk, this should be in the next nightly release and in TypeScript 5.5. The syntax is based on ECMAScript imports: /**
* @import * as foo from "some-module-path"
*/
/**
* @import { x, y as z, default as Default } from "another-module-path"
*/ |
I can't get If I create a I would bet that it's some type of eslint configuration issue but I cannot find any working advice surrounding this topic. |
@turadg Any advice? Am I doing it wrong? (Sorry!) EDIT: Maybe you were reacting to my commenting here at all... Idk, the tag said "Awaiting feedback". However, I wish they would enable discussions here because StackOverflow isn't a great option anymore IMO. |
@waynesbrain I believe the import type { FileTree } from "@/FileTree"; |
Thanks @jespertheend - I see now that the docs aren’t explicit that you can only do this in a JavaScript file, but it’s the only type of file that’s mentioned. I wish they would have implemented it in TS because if I import this type just for documentation using the TS syntax then I get errors for having an unused import. UPDATE: So, I guess the answer for intermodule-y documenting TypeScript |
Background
#22158 tracks referencing types from a given module using JSDoc-style namepaths. Given that the syntax is somewhat unintuitive and predates the concept of ECMAScript modules, we would like to support a more ergonomic form if we feel it would be helpful.
Options
Bikeshedding time. 🚲 🏠
@from
@import
Pros
@from
to precede named import clauses would be easier for completions (this is one of our oldest-duped requests Request for alternative import syntax to improve auto-complete experience #2371)import
clauses@from
before@import
and vice versa.Cons
ECMAScript Import-based
Pros
Cons
Issues with the Above
The options above don't make it explicit that only types are being imported. We could play around with keyword/tag placement (e.g.
@importtype
,@import type
, etc.)The text was updated successfully, but these errors were encountered: