Skip to content

3.3.1 regression in type inference #29743

Open
@eamodio

Description

@eamodio

TypeScript Version: 3.3.1 & 3.4.0-dev.20190202

Search Terms:

Code

class Foo {
    foo: number = 0;
}

class FooBar extends Foo {
    bar: number = 10;
}

async function foo(f: Foo | number) {
    if (f instanceof FooBar) {
        try {
            f = await (async (): Promise<Foo | number> => {
                throw new Error();
            })();
        }
        catch (ex) {
            console.log(f.bar); // 3.3.1 has an error on this line -- "Property 'bar' does not exist on type 'number | Foo'. Property 'bar' does not exist on type 'number'"
        }
    }
}

foo(new FooBar());

Expected behavior:

No error as was in TypeScript 3.2.4

Actual behavior:

Error Property 'bar' does not exist on type 'number | Foo'. Property 'bar' does not exist on type 'number'

Playground Link:
http://www.typescriptlang.org/play/index.html#src=class%20Foo%20%7B%0D%0A%20%20%20%20foo%3A%20number%20%3D%200%3B%0D%0A%7D%0D%0A%0D%0Aclass%20FooBar%20extends%20Foo%20%7B%0D%0A%20%20%20%20bar%3A%20number%20%3D%2010%3B%0D%0A%7D%0D%0A%0D%0Aasync%20function%20foo(f%3A%20Foo%20%7C%20number)%20%7B%0D%0A%20%20%20%20if%20(f%20instanceof%20FooBar)%20%7B%0D%0A%20%20%20%20%20%20%20%20try%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20f%20%3D%20await%20(async%20()%3A%20Promise%3CFoo%20%7C%20number%3E%20%3D%3E%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20throw%20new%20Error()%3B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D)()%3B%0D%0A%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%20%20%20%20catch%20(ex)%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20console.log(f.bar)%3B%0D%0A%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%7D%0D%0A%7D%0D%0A%0D%0Afoo(new%20FooBar())%3B

Related Issues:

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions