Skip to content

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

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

Closed
JeanMeche opened this issue Mar 12, 2023 · 2 comments
Closed

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

JeanMeche opened this issue Mar 12, 2023 · 2 comments

Comments

@JeanMeche
Copy link
Contributor

JeanMeche commented Mar 12, 2023

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

export type Writable<T> = {
    -readonly [K in keyof T]: T[K];
};

class Foo {
    readonly foo = 'foo';
}

class Bar {
    readonly bar = 'bar';
}

type WritableFooBar = Writable<Foo | Bar>;

declare function isFoo(obj: any): obj is Foo;

function test(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

@JeanMeche
Copy link
Contributor Author

Actually, this has been fixed with 5.0 but could find the PR that fixed it !

@RyanCavanaugh
Copy link
Member

#52282

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