microsoft / microsoft/TypeScript

3.3.1 regression in type inference

Open
#29,743 8 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: check: Type Inference
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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:

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the example from the linked TypeScript Playground using TypeScript 3.3.1, then compare its behavior with 3.2.4 and 3.4.0-dev.20190202. Trace how type inference handles the instanceof narrowing across the await assignment and catch block; done means f.bar no longer reports an error in the reported example.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.