Skip to content

Commit 7ca6334

Browse files
authored
Look for outer type parameters on VariableStatements (#37819)
This only applies in JS, where `@template` tags can apply to initialisers of variable declarations: ```js /** * @template T * @returns {(b: T) => T} */ const seq = a => b => b ``` Fixes #36201
1 parent a2609b1 commit 7ca6334

File tree

5 files changed

+100
-0
lines changed

5 files changed

+100
-0
lines changed

src/compiler/checker.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8238,6 +8238,7 @@ namespace ts {
82388238
return undefined;
82398239
}
82408240
switch (node.kind) {
8241+
case SyntaxKind.VariableStatement:
82418242
case SyntaxKind.ClassDeclaration:
82428243
case SyntaxKind.ClassExpression:
82438244
case SyntaxKind.InterfaceDeclaration:
@@ -8265,6 +8266,9 @@ namespace ts {
82658266
else if (node.kind === SyntaxKind.ConditionalType) {
82668267
return concatenate(outerTypeParameters, getInferTypeParameters(<ConditionalTypeNode>node));
82678268
}
8269+
else if (node.kind === SyntaxKind.VariableStatement && !isInJSFile(node)) {
8270+
break;
8271+
}
82688272
const outerAndOwnTypeParameters = appendTypeParameters(outerTypeParameters, getEffectiveTypeParameterDeclarations(<DeclarationWithTypeParameters>node));
82698273
const thisType = includeThisTypes &&
82708274
(node.kind === SyntaxKind.ClassDeclaration || node.kind === SyntaxKind.ClassExpression || node.kind === SyntaxKind.InterfaceDeclaration || isJSConstructor(node)) &&
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//// [instantiateTemplateTagTypeParameterOnVariableStatement.js]
2+
/**
3+
* @template T
4+
* @param {T} a
5+
* @returns {(b: T) => T}
6+
*/
7+
const seq = a => b => b;
8+
9+
const text1 = "hello";
10+
const text2 = "world";
11+
12+
/** @type {string} */
13+
var text3 = seq(text1)(text2);
14+
15+
16+
17+
18+
//// [instantiateTemplateTagTypeParameterOnVariableStatement.d.ts]
19+
declare function seq<T>(a: T): (b: T) => T;
20+
declare const text1: "hello";
21+
declare const text2: "world";
22+
/** @type {string} */
23+
declare var text3: string;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
=== tests/cases/conformance/jsdoc/instantiateTemplateTagTypeParameterOnVariableStatement.js ===
2+
/**
3+
* @template T
4+
* @param {T} a
5+
* @returns {(b: T) => T}
6+
*/
7+
const seq = a => b => b;
8+
>seq : Symbol(seq, Decl(instantiateTemplateTagTypeParameterOnVariableStatement.js, 5, 5))
9+
>a : Symbol(a, Decl(instantiateTemplateTagTypeParameterOnVariableStatement.js, 5, 11))
10+
>b : Symbol(b, Decl(instantiateTemplateTagTypeParameterOnVariableStatement.js, 5, 16))
11+
>b : Symbol(b, Decl(instantiateTemplateTagTypeParameterOnVariableStatement.js, 5, 16))
12+
13+
const text1 = "hello";
14+
>text1 : Symbol(text1, Decl(instantiateTemplateTagTypeParameterOnVariableStatement.js, 7, 5))
15+
16+
const text2 = "world";
17+
>text2 : Symbol(text2, Decl(instantiateTemplateTagTypeParameterOnVariableStatement.js, 8, 5))
18+
19+
/** @type {string} */
20+
var text3 = seq(text1)(text2);
21+
>text3 : Symbol(text3, Decl(instantiateTemplateTagTypeParameterOnVariableStatement.js, 11, 3))
22+
>seq : Symbol(seq, Decl(instantiateTemplateTagTypeParameterOnVariableStatement.js, 5, 5))
23+
>text1 : Symbol(text1, Decl(instantiateTemplateTagTypeParameterOnVariableStatement.js, 7, 5))
24+
>text2 : Symbol(text2, Decl(instantiateTemplateTagTypeParameterOnVariableStatement.js, 8, 5))
25+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
=== tests/cases/conformance/jsdoc/instantiateTemplateTagTypeParameterOnVariableStatement.js ===
2+
/**
3+
* @template T
4+
* @param {T} a
5+
* @returns {(b: T) => T}
6+
*/
7+
const seq = a => b => b;
8+
>seq : <T>(a: T) => (b: T) => T
9+
>a => b => b : <T>(a: T) => (b: T) => T
10+
>a : T
11+
>b => b : (b: T) => T
12+
>b : T
13+
>b : T
14+
15+
const text1 = "hello";
16+
>text1 : "hello"
17+
>"hello" : "hello"
18+
19+
const text2 = "world";
20+
>text2 : "world"
21+
>"world" : "world"
22+
23+
/** @type {string} */
24+
var text3 = seq(text1)(text2);
25+
>text3 : string
26+
>seq(text1)(text2) : string
27+
>seq(text1) : (b: string) => string
28+
>seq : <T>(a: T) => (b: T) => T
29+
>text1 : "hello"
30+
>text2 : "world"
31+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// @checkJs: true
2+
// @allowJs: true
3+
// @declaration: true
4+
// @emitDeclarationOnly: true
5+
// @filename: instantiateTemplateTagTypeParameterOnVariableStatement.js
6+
/**
7+
* @template T
8+
* @param {T} a
9+
* @returns {(b: T) => T}
10+
*/
11+
const seq = a => b => b;
12+
13+
const text1 = "hello";
14+
const text2 = "world";
15+
16+
/** @type {string} */
17+
var text3 = seq(text1)(text2);

0 commit comments

Comments
 (0)