From 945556d5bfd4a7f88140ad4b397bede6dde2db7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Fri, 14 Feb 2025 21:10:56 +0100 Subject: [PATCH] Treat functions with `@type` tags on parameters as typed in JS files --- src/compiler/checker.ts | 1 + .../jsdocTypeTagOnParameter1.symbols | 31 +++++++++++ .../reference/jsdocTypeTagOnParameter1.types | 55 +++++++++++++++++++ .../jsdoc/jsdocTypeTagOnParameter1.ts | 21 +++++++ 4 files changed, 108 insertions(+) create mode 100644 tests/baselines/reference/jsdocTypeTagOnParameter1.symbols create mode 100644 tests/baselines/reference/jsdocTypeTagOnParameter1.types create mode 100644 tests/cases/conformance/jsdoc/jsdocTypeTagOnParameter1.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index d9d1950d6b675..15435a9b75a48 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -15504,6 +15504,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { isInJSFile(declaration) && isValueSignatureDeclaration(declaration) && !hasJSDocParameterTags(declaration) && + !some(declaration.parameters, p => !!getJSDocType(p)) && !getJSDocType(declaration); if (isUntypedSignatureInJSFile) { flags |= SignatureFlags.IsUntypedSignatureInJSFile; diff --git a/tests/baselines/reference/jsdocTypeTagOnParameter1.symbols b/tests/baselines/reference/jsdocTypeTagOnParameter1.symbols new file mode 100644 index 0000000000000..3f6099b16adc2 --- /dev/null +++ b/tests/baselines/reference/jsdocTypeTagOnParameter1.symbols @@ -0,0 +1,31 @@ +//// [tests/cases/conformance/jsdoc/jsdocTypeTagOnParameter1.ts] //// + +=== /index.js === +function repeat( +>repeat : Symbol(repeat, Decl(index.js, 0, 0)) + + /** @type {string} */ message, +>message : Symbol(message, Decl(index.js, 0, 16)) + + /** @type {number} */ times, +>times : Symbol(times, Decl(index.js, 1, 31)) + +) { + return Array(times).fill(message).join(` `); +>Array(times).fill(message).join : Symbol(Array.join, Decl(lib.es5.d.ts, --, --)) +>Array(times).fill : Symbol(Array.fill, Decl(lib.es2015.core.d.ts, --, --)) +>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --) ... and 4 more) +>times : Symbol(times, Decl(index.js, 1, 31)) +>fill : Symbol(Array.fill, Decl(lib.es2015.core.d.ts, --, --)) +>message : Symbol(message, Decl(index.js, 0, 16)) +>join : Symbol(Array.join, Decl(lib.es5.d.ts, --, --)) +} + +/** @type {Parameters[0]} */ +const message = `hello`; +>message : Symbol(message, Decl(index.js, 8, 5)) + +/** @type {Parameters[1]} */ +const times = 3; +>times : Symbol(times, Decl(index.js, 11, 5)) + diff --git a/tests/baselines/reference/jsdocTypeTagOnParameter1.types b/tests/baselines/reference/jsdocTypeTagOnParameter1.types new file mode 100644 index 0000000000000..5b3b0ea8855af --- /dev/null +++ b/tests/baselines/reference/jsdocTypeTagOnParameter1.types @@ -0,0 +1,55 @@ +//// [tests/cases/conformance/jsdoc/jsdocTypeTagOnParameter1.ts] //// + +=== /index.js === +function repeat( +>repeat : (message: string, times: number) => string +> : ^ ^^ ^^ ^^ ^^^^^^^^^^^ + + /** @type {string} */ message, +>message : string +> : ^^^^^^ + + /** @type {number} */ times, +>times : number +> : ^^^^^^ + +) { + return Array(times).fill(message).join(` `); +>Array(times).fill(message).join(` `) : string +> : ^^^^^^ +>Array(times).fill(message).join : (separator?: string) => string +> : ^ ^^^ ^^^^^ +>Array(times).fill(message) : any[] +> : ^^^^^ +>Array(times).fill : (value: any, start?: number, end?: number) => any[] +> : ^ ^^^^^^^ ^^^ ^^ ^^^ ^^^^^^^^^^ +>Array(times) : any[] +> : ^^^^^ +>Array : ArrayConstructor +> : ^^^^^^^^^^^^^^^^ +>times : number +> : ^^^^^^ +>fill : (value: any, start?: number, end?: number) => any[] +> : ^ ^^^^^^^ ^^^ ^^ ^^^ ^^^^^^^^^^ +>message : string +> : ^^^^^^ +>join : (separator?: string) => string +> : ^ ^^^ ^^^^^ +>` ` : " " +> : ^^^ +} + +/** @type {Parameters[0]} */ +const message = `hello`; +>message : string +> : ^^^^^^ +>`hello` : "hello" +> : ^^^^^^^ + +/** @type {Parameters[1]} */ +const times = 3; +>times : number +> : ^^^^^^ +>3 : 3 +> : ^ + diff --git a/tests/cases/conformance/jsdoc/jsdocTypeTagOnParameter1.ts b/tests/cases/conformance/jsdoc/jsdocTypeTagOnParameter1.ts new file mode 100644 index 0000000000000..3dec04c0432a2 --- /dev/null +++ b/tests/cases/conformance/jsdoc/jsdocTypeTagOnParameter1.ts @@ -0,0 +1,21 @@ +// @strict: true +// @lib: esnext +// @allowJS: true +// @checkJs: true +// @noEmit: true + +// https://github.com/microsoft/TypeScript/issues/61172 + +// @filename: /index.js +function repeat( + /** @type {string} */ message, + /** @type {number} */ times, +) { + return Array(times).fill(message).join(` `); +} + +/** @type {Parameters[0]} */ +const message = `hello`; + +/** @type {Parameters[1]} */ +const times = 3;