diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index a078ac45d94..9bcb7dd2fa5 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -12749,12 +12749,14 @@ namespace ts { // Return list of type parameters with duplicates removed (duplicate identifier errors are generated in the actual // type checking functions). - function getTypeParametersFromDeclaration(declaration: DeclarationWithTypeParameters): TypeParameter[] | undefined { + function getTypeParametersFromDeclaration(declaration: DeclarationWithTypeParameters): readonly TypeParameter[] | undefined { let result: TypeParameter[] | undefined; for (const node of getEffectiveTypeParameterDeclarations(declaration)) { result = appendIfUnique(result, getDeclaredTypeOfTypeParameter(node.symbol)); } - return result; + return result?.length ? result + : isFunctionDeclaration(declaration) ? getSignatureOfTypeTag(declaration)?.typeParameters + : undefined; } function symbolsToArray(symbols: SymbolTable): Symbol[] { diff --git a/src/compiler/utilitiesPublic.ts b/src/compiler/utilitiesPublic.ts index f2f0c030840..229620a1191 100644 --- a/src/compiler/utilitiesPublic.ts +++ b/src/compiler/utilitiesPublic.ts @@ -932,6 +932,12 @@ namespace ts { /** * Gets the effective type parameters. If the node was parsed in a * JavaScript file, gets the type parameters from the `@template` tag from JSDoc. + * + * This does *not* return type parameters from a jsdoc reference to a generic type, eg + * + * type Id = (x: T) => T + * /** @type {Id} / + * function id(x) { return x } */ export function getEffectiveTypeParameterDeclarations(node: DeclarationWithTypeParameters): readonly TypeParameterDeclaration[] { if (isJSDocSignature(node)) { diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index 23bfc30ee19..bbc5c38476c 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -4464,6 +4464,12 @@ declare namespace ts { /** * Gets the effective type parameters. If the node was parsed in a * JavaScript file, gets the type parameters from the `@template` tag from JSDoc. + * + * This does *not* return type parameters from a jsdoc reference to a generic type, eg + * + * type Id = (x: T) => T + * /** @type {Id} / + * function id(x) { return x } */ function getEffectiveTypeParameterDeclarations(node: DeclarationWithTypeParameters): readonly TypeParameterDeclaration[]; function getEffectiveConstraintOfTypeParameter(node: TypeParameterDeclaration): TypeNode | undefined; diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index 18ef4ee23f8..f9e5f2d0bde 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -4464,6 +4464,12 @@ declare namespace ts { /** * Gets the effective type parameters. If the node was parsed in a * JavaScript file, gets the type parameters from the `@template` tag from JSDoc. + * + * This does *not* return type parameters from a jsdoc reference to a generic type, eg + * + * type Id = (x: T) => T + * /** @type {Id} / + * function id(x) { return x } */ function getEffectiveTypeParameterDeclarations(node: DeclarationWithTypeParameters): readonly TypeParameterDeclaration[]; function getEffectiveConstraintOfTypeParameter(node: TypeParameterDeclaration): TypeNode | undefined; diff --git a/tests/baselines/reference/typeTagOnFunctionReferencesGeneric.js b/tests/baselines/reference/typeTagOnFunctionReferencesGeneric.js new file mode 100644 index 00000000000..2ae16666077 --- /dev/null +++ b/tests/baselines/reference/typeTagOnFunctionReferencesGeneric.js @@ -0,0 +1,45 @@ +//// [typeTagOnFunctionReferencesGeneric.js] +/** + * @typedef {(m : T) => T} IFn + */ + +/**@type {IFn}*/ +export function inJs(l) { + return l; +} +inJs(1); // lints error. Why? + +/**@type {IFn}*/ +const inJsArrow = (j) => { + return j; +} +inJsArrow(2); // no error gets linted as expected + + +//// [typeTagOnFunctionReferencesGeneric.js] +"use strict"; +/** + * @typedef {(m : T) => T} IFn + */ +exports.__esModule = true; +exports.inJs = void 0; +/**@type {IFn}*/ +function inJs(l) { + return l; +} +exports.inJs = inJs; +inJs(1); // lints error. Why? +/**@type {IFn}*/ +var inJsArrow = function (j) { + return j; +}; +inJsArrow(2); // no error gets linted as expected + + +//// [typeTagOnFunctionReferencesGeneric.d.ts] +/** + * @typedef {(m : T) => T} IFn + */ +/**@type {IFn}*/ +export function inJs(l: T): T; +export type IFn = (m: T) => T; diff --git a/tests/baselines/reference/typeTagOnFunctionReferencesGeneric.symbols b/tests/baselines/reference/typeTagOnFunctionReferencesGeneric.symbols new file mode 100644 index 00000000000..10e9e580697 --- /dev/null +++ b/tests/baselines/reference/typeTagOnFunctionReferencesGeneric.symbols @@ -0,0 +1,27 @@ +=== tests/cases/conformance/salsa/typeTagOnFunctionReferencesGeneric.js === +/** + * @typedef {(m : T) => T} IFn + */ + +/**@type {IFn}*/ +export function inJs(l) { +>inJs : Symbol(inJs, Decl(typeTagOnFunctionReferencesGeneric.js, 0, 0)) +>l : Symbol(l, Decl(typeTagOnFunctionReferencesGeneric.js, 5, 21)) + + return l; +>l : Symbol(l, Decl(typeTagOnFunctionReferencesGeneric.js, 5, 21)) +} +inJs(1); // lints error. Why? +>inJs : Symbol(inJs, Decl(typeTagOnFunctionReferencesGeneric.js, 0, 0)) + +/**@type {IFn}*/ +const inJsArrow = (j) => { +>inJsArrow : Symbol(inJsArrow, Decl(typeTagOnFunctionReferencesGeneric.js, 11, 5)) +>j : Symbol(j, Decl(typeTagOnFunctionReferencesGeneric.js, 11, 19)) + + return j; +>j : Symbol(j, Decl(typeTagOnFunctionReferencesGeneric.js, 11, 19)) +} +inJsArrow(2); // no error gets linted as expected +>inJsArrow : Symbol(inJsArrow, Decl(typeTagOnFunctionReferencesGeneric.js, 11, 5)) + diff --git a/tests/baselines/reference/typeTagOnFunctionReferencesGeneric.types b/tests/baselines/reference/typeTagOnFunctionReferencesGeneric.types new file mode 100644 index 00000000000..eb4f71836a7 --- /dev/null +++ b/tests/baselines/reference/typeTagOnFunctionReferencesGeneric.types @@ -0,0 +1,32 @@ +=== tests/cases/conformance/salsa/typeTagOnFunctionReferencesGeneric.js === +/** + * @typedef {(m : T) => T} IFn + */ + +/**@type {IFn}*/ +export function inJs(l) { +>inJs : (l: T) => T +>l : T + + return l; +>l : T +} +inJs(1); // lints error. Why? +>inJs(1) : 1 +>inJs : (l: T) => T +>1 : 1 + +/**@type {IFn}*/ +const inJsArrow = (j) => { +>inJsArrow : IFn +>(j) => { return j;} : (j: T) => T +>j : T + + return j; +>j : T +} +inJsArrow(2); // no error gets linted as expected +>inJsArrow(2) : 2 +>inJsArrow : IFn +>2 : 2 + diff --git a/tests/cases/conformance/salsa/typeTagOnFunctionReferencesGeneric.ts b/tests/cases/conformance/salsa/typeTagOnFunctionReferencesGeneric.ts new file mode 100644 index 00000000000..945897d3824 --- /dev/null +++ b/tests/cases/conformance/salsa/typeTagOnFunctionReferencesGeneric.ts @@ -0,0 +1,19 @@ +// @checkJs: true +// @declaration: true +// @outDir: out/ +// @filename: typeTagOnFunctionReferencesGeneric.js +/** + * @typedef {(m : T) => T} IFn + */ + +/**@type {IFn}*/ +export function inJs(l) { + return l; +} +inJs(1); // lints error. Why? + +/**@type {IFn}*/ +const inJsArrow = (j) => { + return j; +} +inJsArrow(2); // no error gets linted as expected