Skip to content

Declaration emit breaks with varargs and this type specified in function type parameter #38550

@TimvdLippe

Description

@TimvdLippe

TypeScript Version: 4.0.0-dev.20200512

Search Terms: jsdoc, declaration, varargs

Code

clazz.js:

export class Clazz {
  /**
   * @param {function(this:Object, ...*):*} functionDeclaration
   */
  method(functionDeclaration) {}
}

tsconfig.json:

{
  "compilerOptions": {
    "outDir": "lib",
    "declaration": true,
    "allowJs": true,
    "checkJs": true
  },
  "files": [
    "clazz.js"
  ]
}

Expected behavior:
The emitted declaration file is:

export class Clazz {
    /**
     * @param {function(this:Object, ...*):*} functionDeclaration
     */
    method(functionDeclaration: (...args: any[]) => any): void;
}

Actual behavior:

export class Clazz {
    /**
     * @param {function(this:Object, ...*):*} functionDeclaration
     */
    method(functionDeclaration: (args: any, ...args: any[]) => any): void;
}

Subsequent usages of a composite project break, as args is duplicated in the method argument types.

Note that removal of this:Object fixes the issue. Also, the following properly emits:

export class Clazz {
  /**
   * @param {function(this:Object, ...*):T} functionDeclaration
   * @template T
   */
  method(functionDeclaration) {}
}

Note here that the T is unused in the function, but it fixes the emitted declaration file.

Playground Link:

Related Issues: #38242 (we are successfully able to compile DevTools with the above workaround and the fix for #38242)

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions