Skip to content

Commit 42530c3

Browse files
a-tarasyuksandersn
andauthored
fix(50952): Can't infer from this type on static class method call in JS Doc (#51149)
Co-authored-by: Nathan Shively-Sanders <[email protected]>
1 parent 60edd06 commit 42530c3

File tree

6 files changed

+146
-3
lines changed

6 files changed

+146
-3
lines changed

src/compiler/checker.ts

+7
Original file line numberDiff line numberDiff line change
@@ -14313,6 +14313,13 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1431314313
}
1431414314
}
1431514315

14316+
if (isInJSFile(declaration)) {
14317+
const thisTag = getJSDocThisTag(declaration);
14318+
if (thisTag && thisTag.typeExpression) {
14319+
thisParameter = createSymbolWithType(createSymbol(SymbolFlags.FunctionScopedVariable, InternalSymbolName.This), getTypeFromTypeNode(thisTag.typeExpression));
14320+
}
14321+
}
14322+
1431614323
const classType = declaration.kind === SyntaxKind.Constructor ?
1431714324
getDeclaredTypeOfClassOrInterface(getMergedSymbol((declaration.parent as ClassDeclaration).symbol))
1431814325
: undefined;

tests/baselines/reference/constructorTagWithThisTag.symbols

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ function C() {
99

1010
this.e = this.m + 1
1111
>this.e : Symbol(e, Decl(classthisboth.js, 2, 11))
12-
>this : Symbol(__type, Decl(classthisboth.js, 2, 10))
12+
>this : Symbol(this)
1313
>e : Symbol(C.e, Decl(classthisboth.js, 5, 14))
1414
>this.m : Symbol(m, Decl(classthisboth.js, 2, 22))
15-
>this : Symbol(__type, Decl(classthisboth.js, 2, 10))
15+
>this : Symbol(this)
1616
>m : Symbol(m, Decl(classthisboth.js, 2, 22))
1717
}
1818

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
=== /a.js ===
2+
export class C {
3+
>C : Symbol(C, Decl(a.js, 0, 0))
4+
5+
/**
6+
* @template T
7+
* @this {T}
8+
* @return {T}
9+
*/
10+
static a() {
11+
>a : Symbol(C.a, Decl(a.js, 0, 16))
12+
13+
return this;
14+
>this : Symbol(this)
15+
}
16+
17+
/**
18+
* @template T
19+
* @this {T}
20+
* @return {T}
21+
*/
22+
b() {
23+
>b : Symbol(C.b, Decl(a.js, 8, 5))
24+
25+
return this;
26+
>this : Symbol(this)
27+
}
28+
}
29+
30+
const a = C.a();
31+
>a : Symbol(a, Decl(a.js, 20, 5))
32+
>C.a : Symbol(C.a, Decl(a.js, 0, 16))
33+
>C : Symbol(C, Decl(a.js, 0, 0))
34+
>a : Symbol(C.a, Decl(a.js, 0, 16))
35+
36+
a; // typeof C
37+
>a : Symbol(a, Decl(a.js, 20, 5))
38+
39+
const c = new C();
40+
>c : Symbol(c, Decl(a.js, 23, 5))
41+
>C : Symbol(C, Decl(a.js, 0, 0))
42+
43+
const b = c.b();
44+
>b : Symbol(b, Decl(a.js, 24, 5))
45+
>c.b : Symbol(C.b, Decl(a.js, 8, 5))
46+
>c : Symbol(c, Decl(a.js, 23, 5))
47+
>b : Symbol(C.b, Decl(a.js, 8, 5))
48+
49+
b; // C
50+
>b : Symbol(b, Decl(a.js, 24, 5))
51+
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
=== /a.js ===
2+
export class C {
3+
>C : C
4+
5+
/**
6+
* @template T
7+
* @this {T}
8+
* @return {T}
9+
*/
10+
static a() {
11+
>a : <T>(this: T) => T
12+
13+
return this;
14+
>this : T
15+
}
16+
17+
/**
18+
* @template T
19+
* @this {T}
20+
* @return {T}
21+
*/
22+
b() {
23+
>b : <T>(this: T) => T
24+
25+
return this;
26+
>this : T
27+
}
28+
}
29+
30+
const a = C.a();
31+
>a : typeof C
32+
>C.a() : typeof C
33+
>C.a : <T>(this: T) => T
34+
>C : typeof C
35+
>a : <T>(this: T) => T
36+
37+
a; // typeof C
38+
>a : typeof C
39+
40+
const c = new C();
41+
>c : C
42+
>new C() : C
43+
>C : typeof C
44+
45+
const b = c.b();
46+
>b : C
47+
>c.b() : C
48+
>c.b : <T>(this: T) => T
49+
>c : C
50+
>b : <T>(this: T) => T
51+
52+
b; // C
53+
>b : C
54+

tests/baselines/reference/thisTag1.symbols

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function f(s) {
99

1010
return this.n + s.length
1111
>this.n : Symbol(n, Decl(a.js, 0, 12))
12-
>this : Symbol(__type, Decl(a.js, 0, 11))
12+
>this : Symbol(this)
1313
>n : Symbol(n, Decl(a.js, 0, 12))
1414
>s.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
1515
>s : Symbol(s, Decl(a.js, 4, 11))
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// @noEmit: true
2+
// @allowJs: true
3+
// @checkJs: true
4+
// @filename: /a.js
5+
6+
export class C {
7+
/**
8+
* @template T
9+
* @this {T}
10+
* @return {T}
11+
*/
12+
static a() {
13+
return this;
14+
}
15+
16+
/**
17+
* @template T
18+
* @this {T}
19+
* @return {T}
20+
*/
21+
b() {
22+
return this;
23+
}
24+
}
25+
26+
const a = C.a();
27+
a; // typeof C
28+
29+
const c = new C();
30+
const b = c.b();
31+
b; // C

0 commit comments

Comments
 (0)