Skip to content

Commit 67930fc

Browse files
Don't crash when there's no class type to derive a 'this' type from (#37164)
Fixes #37161
1 parent dfc0b58 commit 67930fc

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

src/compiler/checker.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -21107,8 +21107,10 @@ namespace ts {
2110721107
if (isInJS && className) {
2110821108
const classSymbol = checkExpression(className).symbol;
2110921109
if (classSymbol && classSymbol.members && (classSymbol.flags & SymbolFlags.Function)) {
21110-
const classType = (getDeclaredTypeOfSymbol(classSymbol) as InterfaceType).thisType!;
21111-
return getFlowTypeOfReference(node, classType);
21110+
const classType = (getDeclaredTypeOfSymbol(classSymbol) as InterfaceType).thisType;
21111+
if (classType) {
21112+
return getFlowTypeOfReference(node, classType);
21113+
}
2111221114
}
2111321115
}
2111421116
// Check if it's a constructor definition, can be either a variable decl or function decl
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=== tests/cases/compiler/app.js ===
2+
const f = function() {};
3+
>f : Symbol(f, Decl(app.js, 0, 5))
4+
5+
var g = f;
6+
>g : Symbol(g, Decl(app.js, 1, 3))
7+
>f : Symbol(f, Decl(app.js, 0, 5))
8+
9+
g.prototype.m = function () {
10+
>g.prototype : Symbol(g.m, Decl(app.js, 1, 10))
11+
>g : Symbol(g, Decl(app.js, 1, 3))
12+
>prototype : Symbol(Function.prototype, Decl(lib.es5.d.ts, --, --))
13+
>m : Symbol(g.m, Decl(app.js, 1, 10))
14+
15+
this;
16+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
=== tests/cases/compiler/app.js ===
2+
const f = function() {};
3+
>f : () => void
4+
>function() {} : () => void
5+
6+
var g = f;
7+
>g : () => void
8+
>f : () => void
9+
10+
g.prototype.m = function () {
11+
>g.prototype.m = function () { this;} : () => void
12+
>g.prototype.m : any
13+
>g.prototype : any
14+
>g : () => void
15+
>prototype : any
16+
>m : any
17+
>function () { this;} : () => void
18+
19+
this;
20+
>this : any
21+
22+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// @checkJs: true
2+
// @allowJs: true
3+
// @noEmit: true
4+
5+
// @filename: app.js
6+
const f = function() {};
7+
var g = f;
8+
g.prototype.m = function () {
9+
this;
10+
};

0 commit comments

Comments
 (0)