Skip to content
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

Generating typescript definitions from JSDOC marked javascript fails #41250

Closed
ggreco opened this issue Oct 26, 2020 · 2 comments · Fixed by #41287
Closed

Generating typescript definitions from JSDOC marked javascript fails #41250

ggreco opened this issue Oct 26, 2020 · 2 comments · Fixed by #41287
Labels
Bug A bug in TypeScript Domain: JSDoc Relates to JSDoc parsing and type generation

Comments

@ggreco
Copy link

ggreco commented Oct 26, 2020

TypeScript Version: tried both with 4.0.x and 4.1.0-dev.20201026

Search Terms: jsdoc generate javascript definitions

Code

The goal here is to create a index.d.ts for a library from a JSDoc documented javascript library, the problem is that if the type of an object specified in a file, in this case rectangle.js, is "declared" as parameter or return type in another one:

/**
     * Adds a rectangle
     * 
     * @returns {Rectangle} the rect
     */
  addRectangle() { /* ... */ }

... then an error is emitted since typescript tries to import it again as private symbol (at least this is my interpretation), a workaround pointed in another similar bug (closed) is to replace all the declarations of objects inside the JSDOC markup with something like:

/**
     * Adds a rectangle
     * 
     * @returns {import('./rectangle').Rectangle} the rect
     */
  addRectangle() { /* ... */ }

.... this works, but totally destroy the JSDoc markup of the library.

Simple repo that reproduces the problem (we need 3 files to reproduce it so I thought to keep it external):

https://github.com/ggreco/doc2dec

  • checkout the repo
  • npm install
  • npm run build (or npx tsc)

Expected behavior:

I expect the types to be generated correctly in "types" (index.d.ts and rectangle.d.ts).

Actual behavior:

The compilation fails with this error:
error TS9006: Declaration emit for this file requires using private name 'Rectangle' from module '"/Users/gabry/projects/ts-test/rectangle"'. An explicit type annotation may unblock declaration emit.

index.d.ts is not generated.

Related Issues:
#37832 (closed but not for this particular case)

@Bnaya
Copy link

Bnaya commented Oct 26, 2020

Few notes:
exporting also Rectangle alongside Render does not solve it.

Workaround:
Using import('./rectangle').Rectangle

@weswigham weswigham added Bug A bug in TypeScript Domain: JSDoc Relates to JSDoc parsing and type generation labels Oct 26, 2020
@weswigham
Copy link
Member

Fun fact! In nightly typescript Rectangle in a jsdoc in the example resolves to the static side of the Rectangle class's shape! (What you'd normally think of as typeof Rectangle). This is most certainly an unintended bug, introduced alongside binding const {Thing} = require("./lib") as an alias. What's going wrong is that while the importing statement is an Alias, the exporting statement, module.export = { Rectangle } still isn't, so we return the wrong half of the symbol (using the old jsdoc fallback lookup chicanery, which goes for the static side and not a class instance type because it's a property of an object). @sandersn do we want to "complete the loop" we started by changing how the require statements were bound, as it were, and actually bind module.export = { Rectangle } type statements as Aliases? Or do we want to introduce some strange lookup hacks to allow us to look at it like an Alias event though it's not bound as one (😦)?

That's not the only issue - we're also not painting the visibility of these newly-Alias'd require style imports correctly. I have a fix for that part ready, which can stand on its own (and improves the output of an existing test).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: JSDoc Relates to JSDoc parsing and type generation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants