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

type predicates don't narrow the type of the parent object #43547

Closed
DetachHead opened this issue Apr 6, 2021 · 1 comment
Closed

type predicates don't narrow the type of the parent object #43547

DetachHead opened this issue Apr 6, 2021 · 1 comment

Comments

@DetachHead
Copy link
Contributor

Bug Report

πŸ”Ž Search Terms

πŸ•— Version & Regression Information

4.3.0-dev.20210406

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

declare function isBar(object: unknown): object is Bar

type Bar = {
    qux: number
}

type Foo<T extends Bar | {}> = {
    bar: T
    baz: number
}

declare const foo: Foo<Bar | {}>

declare function fooBar(value: Foo<Bar>): void

if (isBar(foo.bar)) {
    //foo.bar correctly narrowed to Bar:
    const a = foo.bar
    const b = foo.bar.qux

    //foo doesn't get narrowed and is still Foo<{} | Bar>, even though we know it's a Foo<Bar>
    fooBar(foo)
}

πŸ™ Actual behavior

foo.bar is narrowed, but foo remains the same type

πŸ™‚ Expected behavior

foo is narrowed to Foo

@MartinJohns
Copy link
Contributor

Duplicate of #42384. Used search terms: narrow parent

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