Skip to content

Commit eef7c14

Browse files
authored
Treat functions with @type tags on parameters as typed in JS files (#61177)
1 parent 83dc0bb commit eef7c14

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed

src/compiler/checker.ts

+1
Original file line numberDiff line numberDiff line change
@@ -15507,6 +15507,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1550715507
isInJSFile(declaration) &&
1550815508
isValueSignatureDeclaration(declaration) &&
1550915509
!hasJSDocParameterTags(declaration) &&
15510+
!some(declaration.parameters, p => !!getJSDocType(p)) &&
1551015511
!getJSDocType(declaration) &&
1551115512
!getContextualSignatureForFunctionLikeDeclaration(declaration);
1551215513
if (isUntypedSignatureInJSFile) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//// [tests/cases/conformance/jsdoc/jsdocTypeTagOnParameter1.ts] ////
2+
3+
=== /index.js ===
4+
function repeat(
5+
>repeat : Symbol(repeat, Decl(index.js, 0, 0))
6+
7+
/** @type {string} */ message,
8+
>message : Symbol(message, Decl(index.js, 0, 16))
9+
10+
/** @type {number} */ times,
11+
>times : Symbol(times, Decl(index.js, 1, 31))
12+
13+
) {
14+
return Array(times).fill(message).join(` `);
15+
>Array(times).fill(message).join : Symbol(Array.join, Decl(lib.es5.d.ts, --, --))
16+
>Array(times).fill : Symbol(Array.fill, Decl(lib.es2015.core.d.ts, --, --))
17+
>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)
18+
>times : Symbol(times, Decl(index.js, 1, 31))
19+
>fill : Symbol(Array.fill, Decl(lib.es2015.core.d.ts, --, --))
20+
>message : Symbol(message, Decl(index.js, 0, 16))
21+
>join : Symbol(Array.join, Decl(lib.es5.d.ts, --, --))
22+
}
23+
24+
/** @type {Parameters<typeof repeat>[0]} */
25+
const message = `hello`;
26+
>message : Symbol(message, Decl(index.js, 8, 5))
27+
28+
/** @type {Parameters<typeof repeat>[1]} */
29+
const times = 3;
30+
>times : Symbol(times, Decl(index.js, 11, 5))
31+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//// [tests/cases/conformance/jsdoc/jsdocTypeTagOnParameter1.ts] ////
2+
3+
=== /index.js ===
4+
function repeat(
5+
>repeat : (message: string, times: number) => string
6+
> : ^ ^^ ^^ ^^ ^^^^^^^^^^^
7+
8+
/** @type {string} */ message,
9+
>message : string
10+
> : ^^^^^^
11+
12+
/** @type {number} */ times,
13+
>times : number
14+
> : ^^^^^^
15+
16+
) {
17+
return Array(times).fill(message).join(` `);
18+
>Array(times).fill(message).join(` `) : string
19+
> : ^^^^^^
20+
>Array(times).fill(message).join : (separator?: string) => string
21+
> : ^ ^^^ ^^^^^
22+
>Array(times).fill(message) : any[]
23+
> : ^^^^^
24+
>Array(times).fill : (value: any, start?: number, end?: number) => any[]
25+
> : ^ ^^^^^^^ ^^^ ^^ ^^^ ^^^^^^^^^^
26+
>Array(times) : any[]
27+
> : ^^^^^
28+
>Array : ArrayConstructor
29+
> : ^^^^^^^^^^^^^^^^
30+
>times : number
31+
> : ^^^^^^
32+
>fill : (value: any, start?: number, end?: number) => any[]
33+
> : ^ ^^^^^^^ ^^^ ^^ ^^^ ^^^^^^^^^^
34+
>message : string
35+
> : ^^^^^^
36+
>join : (separator?: string) => string
37+
> : ^ ^^^ ^^^^^
38+
>` ` : " "
39+
> : ^^^
40+
}
41+
42+
/** @type {Parameters<typeof repeat>[0]} */
43+
const message = `hello`;
44+
>message : string
45+
> : ^^^^^^
46+
>`hello` : "hello"
47+
> : ^^^^^^^
48+
49+
/** @type {Parameters<typeof repeat>[1]} */
50+
const times = 3;
51+
>times : number
52+
> : ^^^^^^
53+
>3 : 3
54+
> : ^
55+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// @strict: true
2+
// @lib: esnext
3+
// @allowJS: true
4+
// @checkJs: true
5+
// @noEmit: true
6+
7+
// https://github.com/microsoft/TypeScript/issues/61172
8+
9+
// @filename: /index.js
10+
function repeat(
11+
/** @type {string} */ message,
12+
/** @type {number} */ times,
13+
) {
14+
return Array(times).fill(message).join(` `);
15+
}
16+
17+
/** @type {Parameters<typeof repeat>[0]} */
18+
const message = `hello`;
19+
20+
/** @type {Parameters<typeof repeat>[1]} */
21+
const times = 3;

0 commit comments

Comments
 (0)