Skip to content

instanceof type guard does not narrow type of this #5534

Closed
@sccolbert

Description

@sccolbert

Here's an example which demonstrates (tested on 1.6.2 and current playground):

class Foo {
    a: number;

    foo(): string {
        if (this instanceof Bar) {
            return this.b;  // not okay - `this` typed as Foo
        }
        return '';
    }

    bar(): string {
        let that = this;
        if (that instanceof Bar) {
            return that.b;  // okay - `that` typed as Bar
        }
        return ''
    }
}


class Bar extends Foo {
    b: string;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptFixedA PR has been merged for this issueHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions