diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 0dd8de54a1..8593defd1a 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -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 diff --git a/tests/baselines/reference/allowJsClassThisTypeCrash.symbols b/tests/baselines/reference/allowJsClassThisTypeCrash.symbols new file mode 100644 index 0000000000..2f72ed43e2 --- /dev/null +++ b/tests/baselines/reference/allowJsClassThisTypeCrash.symbols @@ -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; +}; diff --git a/tests/baselines/reference/allowJsClassThisTypeCrash.types b/tests/baselines/reference/allowJsClassThisTypeCrash.types new file mode 100644 index 0000000000..7c136950f3 --- /dev/null +++ b/tests/baselines/reference/allowJsClassThisTypeCrash.types @@ -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 + +}; diff --git a/tests/cases/compiler/allowJsClassThisTypeCrash.ts b/tests/cases/compiler/allowJsClassThisTypeCrash.ts new file mode 100644 index 0000000000..bec30d8960 --- /dev/null +++ b/tests/cases/compiler/allowJsClassThisTypeCrash.ts @@ -0,0 +1,10 @@ +// @checkJs: true +// @allowJs: true +// @noEmit: true + +// @filename: app.js +const f = function() {}; +var g = f; +g.prototype.m = function () { + this; +}; \ No newline at end of file