Skip to content

JSDoc @overloads on constructors don't capture class type parameters, can dictate their return type #55919

Description

Found from #55916.

Today if you try to write a generic constructor, captured type parameters fail to create a generic signature. Instead, the type parameter is effectively leaked and never instantiated.

/** * @template T */classC{/** * @overload * @param {T} value *//** * @overload * @param {T} first * @param {T} second *//** * @param {T} first * @param {T} [second] */constructor(first,second){}}constx=newC(123);// ~~~// Argument of type 'number' is not assignable to parameter of type 'T'.// 'T' could be instantiated with an arbitrary type which could be unrelated to 'number'.consty=newC("hello","world");// ~~~~~~~// Argument of type 'string' is not assignable to parameter of type 'T'.// 'T' could be instantiated with an arbitrary type which could be unrelated to 'string'.

The current-workaround is to write something like the following:

/** * @template T */classC{/** * @template T * @overload * @param {T} value * @return {C<T>} *//** * @template T * @overload * @param {T} first * @param {T} second * @return {C<T>} *//** * @param {T} first * @param {T} [second] */constructor(first,second){}}constx=newC(123);// ^?consty=newC("hello","world");// ^?consty=newC(123,"hello");// ^?

Notice that here, each @overload must declare its own type parameters and return type.

We should decide if this is actually how we want @overload to work in JSDoc on constructor declarations.

Metadata

Metadata

Labels

Domain: JSDocRelates to JSDoc parsing and type generationDomain: JavaScriptThe issue relates to JavaScript specificallyNeeds InvestigationThis issue needs a team member to investigate its status.checkJsRelates to checking JavaScript using TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions