diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 52a667a145..e1cda181ee 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -31909,7 +31909,9 @@ namespace ts { let childExpression = childNode.parent; while (testedExpression && childExpression) { - if (isIdentifier(testedExpression) && isIdentifier(childExpression)) { + if (isIdentifier(testedExpression) && isIdentifier(childExpression) || + testedExpression.kind === SyntaxKind.ThisKeyword && childExpression.kind === SyntaxKind.ThisKeyword + ) { return getSymbolAtLocation(testedExpression) === getSymbolAtLocation(childExpression); } diff --git a/tests/baselines/reference/truthinessCallExpressionCoercion1.errors.txt b/tests/baselines/reference/truthinessCallExpressionCoercion1.errors.txt index 6a32d1f050..e30041e220 100644 --- a/tests/baselines/reference/truthinessCallExpressionCoercion1.errors.txt +++ b/tests/baselines/reference/truthinessCallExpressionCoercion1.errors.txt @@ -80,6 +80,11 @@ tests/cases/compiler/truthinessCallExpressionCoercion1.ts(61,9): error TS2774: T // ok this.maybeIsUser ? console.log('this.maybeIsUser') : undefined; + + // ok + if (this.isUser) { + this.isUser(); + } } } \ No newline at end of file diff --git a/tests/baselines/reference/truthinessCallExpressionCoercion1.js b/tests/baselines/reference/truthinessCallExpressionCoercion1.js index 4415cedf59..ecb76aadf8 100644 --- a/tests/baselines/reference/truthinessCallExpressionCoercion1.js +++ b/tests/baselines/reference/truthinessCallExpressionCoercion1.js @@ -63,6 +63,11 @@ class Foo { // ok this.maybeIsUser ? console.log('this.maybeIsUser') : undefined; + + // ok + if (this.isUser) { + this.isUser(); + } } } @@ -117,6 +122,10 @@ var Foo = /** @class */ (function () { this.isUser ? console.log('this.isUser') : undefined; // ok this.maybeIsUser ? console.log('this.maybeIsUser') : undefined; + // ok + if (this.isUser) { + this.isUser(); + } }; return Foo; }()); diff --git a/tests/baselines/reference/truthinessCallExpressionCoercion1.symbols b/tests/baselines/reference/truthinessCallExpressionCoercion1.symbols index 1f729481ac..aace0d3c55 100644 --- a/tests/baselines/reference/truthinessCallExpressionCoercion1.symbols +++ b/tests/baselines/reference/truthinessCallExpressionCoercion1.symbols @@ -170,6 +170,18 @@ class Foo { >console : Symbol(console, Decl(lib.dom.d.ts, --, --)) >log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) >undefined : Symbol(undefined) + + // ok + if (this.isUser) { +>this.isUser : Symbol(Foo.isUser, Decl(truthinessCallExpressionCoercion1.ts, 52, 32)) +>this : Symbol(Foo, Decl(truthinessCallExpressionCoercion1.ts, 49, 1)) +>isUser : Symbol(Foo.isUser, Decl(truthinessCallExpressionCoercion1.ts, 52, 32)) + + this.isUser(); +>this.isUser : Symbol(Foo.isUser, Decl(truthinessCallExpressionCoercion1.ts, 52, 32)) +>this : Symbol(Foo, Decl(truthinessCallExpressionCoercion1.ts, 49, 1)) +>isUser : Symbol(Foo.isUser, Decl(truthinessCallExpressionCoercion1.ts, 52, 32)) + } } } diff --git a/tests/baselines/reference/truthinessCallExpressionCoercion1.types b/tests/baselines/reference/truthinessCallExpressionCoercion1.types index 8c53f481c4..bcbde0d35f 100644 --- a/tests/baselines/reference/truthinessCallExpressionCoercion1.types +++ b/tests/baselines/reference/truthinessCallExpressionCoercion1.types @@ -223,6 +223,19 @@ class Foo { >log : (...data: any[]) => void >'this.maybeIsUser' : "this.maybeIsUser" >undefined : undefined + + // ok + if (this.isUser) { +>this.isUser : () => boolean +>this : this +>isUser : () => boolean + + this.isUser(); +>this.isUser() : boolean +>this.isUser : () => boolean +>this : this +>isUser : () => boolean + } } } diff --git a/tests/cases/compiler/truthinessCallExpressionCoercion1.ts b/tests/cases/compiler/truthinessCallExpressionCoercion1.ts index 04331cbead..6835a434aa 100644 --- a/tests/cases/compiler/truthinessCallExpressionCoercion1.ts +++ b/tests/cases/compiler/truthinessCallExpressionCoercion1.ts @@ -64,5 +64,10 @@ class Foo { // ok this.maybeIsUser ? console.log('this.maybeIsUser') : undefined; + + // ok + if (this.isUser) { + this.isUser(); + } } }