From 7f3ca9f8f7066256101b34fb7df7ec57999c38fc Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> Date: Tue, 19 Jul 2022 15:40:53 -0700 Subject: [PATCH] Copy type parameters to function with @type tag (#49960) * Copy type parameters to function with @type tag Previously, type references to generic types would fail to copy type parameters from type references in an `@type` tag. Now the type parameter is copied. Note that I left 3 places in the checker unchanged, even though they should technically also get type parameters from type refereneces: 1. getOuterTypeParameters -- not fixing this means that nested function still won't get instantiated correctly. I'll see how hard this is to fix. 2. getLocalTypeParameterOfClassOrInterfaceOrTypeAlias -- only applies to constructor functions which have a type annotation, which doesn't type check in the first place. 3. isThislessType -- This function is conservative, so should be possible to make it more conservative without writing a lot of code. Fixes #49039 * Update API baselines --- src/compiler/checker.ts | 6 ++- src/compiler/utilitiesPublic.ts | 6 +++ .../reference/api/tsserverlibrary.d.ts | 6 +++ tests/baselines/reference/api/typescript.d.ts | 6 +++ .../typeTagOnFunctionReferencesGeneric.js | 45 +++++++++++++++++++ ...typeTagOnFunctionReferencesGeneric.symbols | 27 +++++++++++ .../typeTagOnFunctionReferencesGeneric.types | 32 +++++++++++++ .../typeTagOnFunctionReferencesGeneric.ts | 19 ++++++++ 8 files changed, 145 insertions(+), 2 deletions(-) create mode 100644 tests/baselines/reference/typeTagOnFunctionReferencesGeneric.js create mode 100644 tests/baselines/reference/typeTagOnFunctionReferencesGeneric.symbols create mode 100644 tests/baselines/reference/typeTagOnFunctionReferencesGeneric.types create mode 100644 tests/cases/conformance/salsa/typeTagOnFunctionReferencesGeneric.ts 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