Skip to content

Generating typescript definitions from JSDOC marked javascript fails #41250

Closed
@ggreco

Description

@ggreco

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: JSDocRelates to JSDoc parsing and type generation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions