Skip to content

Narrow by constant element access callees #62314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1295,8 +1295,8 @@ function createBinder(): (file: SourceFile, options: CompilerOptions) => void {
}
}
if (
expr.expression.kind === SyntaxKind.PropertyAccessExpression &&
containsNarrowableReference((expr.expression as PropertyAccessExpression).expression)
(expr.expression.kind === SyntaxKind.PropertyAccessExpression || expr.expression.kind === SyntaxKind.ElementAccessExpression) &&
containsNarrowableReference((expr.expression as PropertyAccessExpression | ElementAccessExpression).expression)
) {
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27970,8 +27970,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
}
if (
expression.expression.kind === SyntaxKind.PropertyAccessExpression &&
isOrContainsMatchingReference(reference, (expression.expression as PropertyAccessExpression).expression)
(expression.expression.kind === SyntaxKind.PropertyAccessExpression || expression.expression.kind === SyntaxKind.ElementAccessExpression && isConstantReference((expression.expression as ElementAccessExpression).argumentExpression)) &&
isOrContainsMatchingReference(reference, (expression.expression as PropertyAccessExpression | ElementAccessExpression).expression)
) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//// [tests/cases/compiler/narrowByTypePredicateFromElementAccessExpression1.ts] ////

=== narrowByTypePredicateFromElementAccessExpression1.ts ===
// https://github.com/microsoft/TypeScript/issues/62247

const isBar = Symbol("isBar");
>isBar : Symbol(isBar, Decl(narrowByTypePredicateFromElementAccessExpression1.ts, 2, 5))
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2019.symbol.d.ts, --, --))

abstract class Foo {
>Foo : Symbol(Foo, Decl(narrowByTypePredicateFromElementAccessExpression1.ts, 2, 30))

abstract [isBar](): this is Bar;
>[isBar] : Symbol(Foo[isBar], Decl(narrowByTypePredicateFromElementAccessExpression1.ts, 4, 20))
>isBar : Symbol(isBar, Decl(narrowByTypePredicateFromElementAccessExpression1.ts, 2, 5))
>Bar : Symbol(Bar, Decl(narrowByTypePredicateFromElementAccessExpression1.ts, 12, 1))

method(): void {
>method : Symbol(Foo.method, Decl(narrowByTypePredicateFromElementAccessExpression1.ts, 5, 34))

if (this[isBar]()) {
>this : Symbol(Foo, Decl(narrowByTypePredicateFromElementAccessExpression1.ts, 2, 30))
>isBar : Symbol(isBar, Decl(narrowByTypePredicateFromElementAccessExpression1.ts, 2, 5))

this.barMethod(); // ok
>this.barMethod : Symbol(Bar.barMethod, Decl(narrowByTypePredicateFromElementAccessExpression1.ts, 17, 3))
>barMethod : Symbol(Bar.barMethod, Decl(narrowByTypePredicateFromElementAccessExpression1.ts, 17, 3))
}
}
}

class Bar extends Foo {
>Bar : Symbol(Bar, Decl(narrowByTypePredicateFromElementAccessExpression1.ts, 12, 1))
>Foo : Symbol(Foo, Decl(narrowByTypePredicateFromElementAccessExpression1.ts, 2, 30))

override [isBar](): this is Bar {
>[isBar] : Symbol(Bar[isBar], Decl(narrowByTypePredicateFromElementAccessExpression1.ts, 14, 23))
>isBar : Symbol(isBar, Decl(narrowByTypePredicateFromElementAccessExpression1.ts, 2, 5))
>Bar : Symbol(Bar, Decl(narrowByTypePredicateFromElementAccessExpression1.ts, 12, 1))

return true;
}

barMethod(): void {}
>barMethod : Symbol(Bar.barMethod, Decl(narrowByTypePredicateFromElementAccessExpression1.ts, 17, 3))
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//// [tests/cases/compiler/narrowByTypePredicateFromElementAccessExpression1.ts] ////

=== narrowByTypePredicateFromElementAccessExpression1.ts ===
// https://github.com/microsoft/TypeScript/issues/62247

const isBar = Symbol("isBar");
>isBar : unique symbol
> : ^^^^^^^^^^^^^
>Symbol("isBar") : unique symbol
> : ^^^^^^^^^^^^^
>Symbol : SymbolConstructor
> : ^^^^^^^^^^^^^^^^^
>"isBar" : "isBar"
> : ^^^^^^^

abstract class Foo {
>Foo : Foo
> : ^^^

abstract [isBar](): this is Bar;
>[isBar] : () => this is Bar
> : ^^^^^^
>isBar : unique symbol
> : ^^^^^^^^^^^^^

method(): void {
>method : () => void
> : ^^^^^^

if (this[isBar]()) {
>this[isBar]() : boolean
> : ^^^^^^^
>this[isBar] : () => this is Bar
> : ^^^^^^
>this : this
> : ^^^^
>isBar : unique symbol
> : ^^^^^^^^^^^^^

this.barMethod(); // ok
>this.barMethod() : void
> : ^^^^
>this.barMethod : () => void
> : ^^^^^^
>this : this & Bar
> : ^^^^^^^^^^
>barMethod : () => void
> : ^^^^^^
}
}
}

class Bar extends Foo {
>Bar : Bar
> : ^^^
>Foo : Foo
> : ^^^

override [isBar](): this is Bar {
>[isBar] : () => this is Bar
> : ^^^^^^
>isBar : unique symbol
> : ^^^^^^^^^^^^^

return true;
>true : true
> : ^^^^
}

barMethod(): void {}
>barMethod : () => void
> : ^^^^^^
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// @strict: true
// @target: esnext
// @noEmit: true

// https://github.com/microsoft/TypeScript/issues/62247

const isBar = Symbol("isBar");

abstract class Foo {
abstract [isBar](): this is Bar;

method(): void {
if (this[isBar]()) {
this.barMethod(); // ok
}
}
}

class Bar extends Foo {
override [isBar](): this is Bar {
return true;
}

barMethod(): void {}
}
Loading