Don't crash when there's no class type to derive a 'this' type from (#37164)

Fixes #37161
This commit is contained in:
Ryan Cavanaugh 2020-03-04 09:34:28 -08:00 committed by GitHub
parent dfc0b58fe7
commit 67930fc163
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 2 deletions

View file

@ -21107,8 +21107,10 @@ namespace ts {
if (isInJS && className) {
const classSymbol = checkExpression(className).symbol;
if (classSymbol && classSymbol.members && (classSymbol.flags & SymbolFlags.Function)) {
const classType = (getDeclaredTypeOfSymbol(classSymbol) as InterfaceType).thisType!;
return getFlowTypeOfReference(node, classType);
const classType = (getDeclaredTypeOfSymbol(classSymbol) as InterfaceType).thisType;
if (classType) {
return getFlowTypeOfReference(node, classType);
}
}
}
// Check if it's a constructor definition, can be either a variable decl or function decl

View file

@ -0,0 +1,16 @@
=== tests/cases/compiler/app.js ===
const f = function() {};
>f : Symbol(f, Decl(app.js, 0, 5))
var g = f;
>g : Symbol(g, Decl(app.js, 1, 3))
>f : Symbol(f, Decl(app.js, 0, 5))
g.prototype.m = function () {
>g.prototype : Symbol(g.m, Decl(app.js, 1, 10))
>g : Symbol(g, Decl(app.js, 1, 3))
>prototype : Symbol(Function.prototype, Decl(lib.es5.d.ts, --, --))
>m : Symbol(g.m, Decl(app.js, 1, 10))
this;
};

View file

@ -0,0 +1,22 @@
=== tests/cases/compiler/app.js ===
const f = function() {};
>f : () => void
>function() {} : () => void
var g = f;
>g : () => void
>f : () => void
g.prototype.m = function () {
>g.prototype.m = function () { this;} : () => void
>g.prototype.m : any
>g.prototype : any
>g : () => void
>prototype : any
>m : any
>function () { this;} : () => void
this;
>this : any
};

View file

@ -0,0 +1,10 @@
// @checkJs: true
// @allowJs: true
// @noEmit: true
// @filename: app.js
const f = function() {};
var g = f;
g.prototype.m = function () {
this;
};