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

TypeScript should respect "instanceof" in logical expressions #31291

Closed
slavafomin opened this issue May 7, 2019 · 3 comments · Fixed by #44730
Closed

TypeScript should respect "instanceof" in logical expressions #31291

slavafomin opened this issue May 7, 2019 · 3 comments · Fixed by #44730
Labels
Duplicate An existing issue was already created

Comments

@slavafomin
Copy link

Hello!

Thank you for your hard work.

Please, consider this code:

class Foo { 
    foo: string;
}
class Bar {
    bar: string;
}

function foo(value: Foo | Bar) {
    const isFoo = value instanceof Foo;
    const isBar = !isFoo && value.bar; // <-- causes error
}

The line above throws an error:

Property 'bar' does not exist on type 'Foo | Bar'.
Property 'bar' does not exist on type 'Foo'.

However, if I replace the logical variable with the expression directly, it works:

const isBar = !(value instanceof Foo) && value.bar;

The isFoo variable in my example is a boolean constant, it's value can't change, so I believe it should be statically analyzable.

@j-oliveras
Copy link
Contributor

Typescript don't track related variables.

@JakeTunaley
Copy link

Duplicate of #12184

@weswigham weswigham added the Duplicate An existing issue was already created label May 8, 2019
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants
@slavafomin @weswigham @j-oliveras @typescript-bot @JakeTunaley and others