diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index ac8a47a9913..054983f0d97 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -3147,23 +3147,29 @@ namespace ts { if (declaration.kind === SyntaxKind.ExportAssignment) { return links.type = checkExpression((declaration).expression); } - // Handle module.exports = expr - if (declaration.kind === SyntaxKind.BinaryExpression) { - return links.type = getUnionType(map(symbol.declarations, (decl: BinaryExpression) => checkExpressionCached(decl.right))); - } - if (declaration.kind === SyntaxKind.PropertyAccessExpression) { - // Declarations only exist for property access expressions for certain - // special assignment kinds - if (declaration.parent.kind === SyntaxKind.BinaryExpression) { - // Handle exports.p = expr or this.p = expr or className.prototype.method = expr - return links.type = checkExpressionCached((declaration.parent).right); - } - } // Handle variable, parameter or property if (!pushTypeResolution(symbol, TypeSystemPropertyName.Type)) { return unknownType; } - let type = getWidenedTypeForVariableLikeDeclaration(declaration, /*reportErrors*/ true); + + let type: Type = undefined; + // Handle module.exports = expr or this.p = expr + if (declaration.kind === SyntaxKind.BinaryExpression) { + type = getUnionType(map(symbol.declarations, (decl: BinaryExpression) => checkExpressionCached(decl.right))); + } + else if (declaration.kind === SyntaxKind.PropertyAccessExpression) { + // Declarations only exist for property access expressions for certain + // special assignment kinds + if (declaration.parent.kind === SyntaxKind.BinaryExpression) { + // Handle exports.p = expr or className.prototype.method = expr + type = checkExpressionCached((declaration.parent).right); + } + } + + if (type === undefined) { + type = getWidenedTypeForVariableLikeDeclaration(declaration, /*reportErrors*/ true); + } + if (!popTypeResolution()) { if ((symbol.valueDeclaration).type) { // Variable has type annotation that circularly references the variable itself diff --git a/tests/baselines/reference/jsFileClassSelfReferencedProperty.symbols b/tests/baselines/reference/jsFileClassSelfReferencedProperty.symbols new file mode 100644 index 00000000000..2d9626897bf --- /dev/null +++ b/tests/baselines/reference/jsFileClassSelfReferencedProperty.symbols @@ -0,0 +1,17 @@ +=== tests/cases/compiler/foo.js === + +export class StackOverflowTest { +>StackOverflowTest : Symbol(StackOverflowTest, Decl(foo.js, 0, 0)) + + constructor () { + this.testStackOverflow = this.testStackOverflow.bind(this) +>this.testStackOverflow : Symbol(StackOverflowTest.testStackOverflow, Decl(foo.js, 2, 18)) +>this : Symbol(StackOverflowTest, Decl(foo.js, 0, 0)) +>testStackOverflow : Symbol(StackOverflowTest.testStackOverflow, Decl(foo.js, 2, 18)) +>this.testStackOverflow : Symbol(StackOverflowTest.testStackOverflow, Decl(foo.js, 2, 18)) +>this : Symbol(StackOverflowTest, Decl(foo.js, 0, 0)) +>testStackOverflow : Symbol(StackOverflowTest.testStackOverflow, Decl(foo.js, 2, 18)) +>this : Symbol(StackOverflowTest, Decl(foo.js, 0, 0)) + } +} + diff --git a/tests/baselines/reference/jsFileClassSelfReferencedProperty.types b/tests/baselines/reference/jsFileClassSelfReferencedProperty.types new file mode 100644 index 00000000000..44344ac1472 --- /dev/null +++ b/tests/baselines/reference/jsFileClassSelfReferencedProperty.types @@ -0,0 +1,21 @@ +=== tests/cases/compiler/foo.js === + +export class StackOverflowTest { +>StackOverflowTest : StackOverflowTest + + constructor () { + this.testStackOverflow = this.testStackOverflow.bind(this) +>this.testStackOverflow = this.testStackOverflow.bind(this) : any +>this.testStackOverflow : any +>this : this +>testStackOverflow : any +>this.testStackOverflow.bind(this) : any +>this.testStackOverflow.bind : any +>this.testStackOverflow : any +>this : this +>testStackOverflow : any +>bind : any +>this : this + } +} + diff --git a/tests/cases/compiler/jsFileClassSelfReferencedProperty.ts b/tests/cases/compiler/jsFileClassSelfReferencedProperty.ts new file mode 100644 index 00000000000..8d6cecdb200 --- /dev/null +++ b/tests/cases/compiler/jsFileClassSelfReferencedProperty.ts @@ -0,0 +1,9 @@ +// @allowJs: true +// @noEmit: true + +// @filename: foo.js +export class StackOverflowTest { + constructor () { + this.testStackOverflow = this.testStackOverflow.bind(this) + } +}