Skip to content

TS2304 when a generic (@template) function JSDoc contains @overload #4037

@antichris

Description

@antichris

Generic functions that have an @overload in their JSDoc produce TS2304: Cannot find name 'T' errors.

In const assignments this applies only to the base params (the params in all the overloads are fine). In function definitions this applies to all params except those of the first overload.

CHANGES.md does not address this new "feature".

Steps to reproduce

const createElementC = /**
	 * @template {keyof HTMLElementTagNameMap} T
	 * @param {T}t
	 * @param {NodeList|HTMLCollection=}c
	 *
	 * @overload
	 * @param {T}t
	 * @return {HTMLElementTagNameMap[T]}
	 *
	 * @overload
	 * @param {T}t
	 * @param {NodeList|HTMLCollection}c
	 * @return {HTMLElementTagNameMap[T]}
	 */(t, c) => {
		/* ... omitted for brevity ... */ return document.createElement(t)
	}

/**
 * @template {keyof HTMLElementTagNameMap} T
 * @param {T}t
 * @param {NodeList|HTMLCollection=}c
 *
 * @overload
 * @param {T}t
 * @return {HTMLElementTagNameMap[T]}
 *
 * @overload
 * @param {T}t
 * @param {NodeList|HTMLCollection}c
 * @return {HTMLElementTagNameMap[T]}
 */
function createElementF(t, c) {
	/* ... omitted for brevity ... */ return document.createElement(t)
}

The real use case is, of course, far more complex than the repro above.

Behavior with typescript@6.0

tsc --checkJs --strict --noEmit index.js

No output, no errors.

Behavior with tsgo

tsgo --checkJs --strict --noEmit index.js
index.js:5:13 - error TS2304: Cannot find name 'T'.

5   * @param {T}t
              ~

index.js:22:12 - error TS2304: Cannot find name 'T'.

22  * @param {T}t
              ~

index.js:30:12 - error TS2304: Cannot find name 'T'.

30  * @param {T}t
              ~

index.js:32:35 - error TS2304: Cannot find name 'T'.

32  * @return {HTMLElementTagNameMap[T]}
                                     ~

index.js:32:35 - error TS2538: Type 'T' cannot be used as an index type.

32  * @return {HTMLElementTagNameMap[T]}
                                     ~


Found 5 errors in the same file, starting at: index.js:5

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions