Skip to content

Broken type inference with a typeguard in a if statement #53216

Description

Bug Report

🔎 Search Terms

  • type guard
  • inference
  • if condition

🕗 Version & Regression Information

This is a regression.

Inference was OK on 4.7.4 but broken on 4.8.4 and 4.9.5 (tested on the playground).

⏯ Playground Link

Playground link with relevant code

💻 Code

exporttypeWritable<T>={-readonly[KinkeyofT]: T[K];};classFoo{readonlyfoo='foo';}classBar{readonlybar='bar';}typeWritableFooBar=Writable<Foo|Bar>;declarefunctionisFoo(obj: any): obj is Foo;functiontest(foobar: WritableFooBar): void{if(!isFoo(foobar)){}// happens also with if(isFoo())foobar// ^? Foo | Writable<Bar>}

🙁 Actual behavior

foobar is inference as Foo | Writable<Bar>

🙂 Expected behavior

foobar should be inferred as Writable<Foo|Bar> or WritableFooBar

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions