Skip to content
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

Existing check is lost after type union discrimination #32598

Closed
princed opened this issue Jul 29, 2019 · 2 comments
Closed

Existing check is lost after type union discrimination #32598

princed opened this issue Jul 29, 2019 · 2 comments

Comments

@princed
Copy link

princed commented Jul 29, 2019

TypeScript Version: 2.4.1, 3.5.1, 3.6.0-dev.20190727

Search Terms:
type union discrimination, narrowing, widening

Code

interface Base { common?: string }

interface Sub1 extends Base { type: 'sub1' }

interface Sub2 extends Base { type: 'sub2' }

type Union = Sub1 | Sub2;

function test(u: Union) {
    if (u.common) {
        if (u.type === "sub1") {
			u.common.toString(); // <-- Object is possibly 'undefined'.
        }
    }
}

Expected behavior:
No error message, since u.common is already proved to exist.

Actual behavior:
Error message

Playground Link:
Playground link

Related Issues:
The closest report I could find was #11787 (comment), but apparently, it's related to type guards, which impose some design limitation as far as I understand.

@fatcerberus
Copy link

Duplicate of #31495 (among others).

@princed
Copy link
Author

princed commented Jul 29, 2019

Oh, I see, thanks.

@princed princed closed this as completed Jul 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants