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

Unsound type inference with in operator on union #34975

Closed
kaya3 opened this issue Nov 7, 2019 · 2 comments
Closed

Unsound type inference with in operator on union #34975

kaya3 opened this issue Nov 7, 2019 · 2 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@kaya3
Copy link

kaya3 commented Nov 7, 2019

TypeScript Version: 3.8.0-dev.20191105

Search Terms: in operator, union, unsound

Code

type Union = { foo: string; foo2: string } | { bar: number; bar2: number };

const union: Union = { foo: 'a', bar: 1, bar2: 2 };

if('foo' in union) {
    union; // incorrectly inferred as { foo: string, foo2: string }
}

Expected behavior: Type of union should be narrowed to Union & { foo: any }, or not narrowed.

Actual behavior: Type of union is narrowed to { foo: string, foo2: string } despite having no foo2 property.

Playground Link: http://www.typescriptlang.org/play/?ts=3.8.0-dev.20191105&ssl=1&ssc=1&pln=8&pc=1#code/C4TwDgpgBAqgdgSwPZygXigbygMyUgLigGdgAnBOAcwG5d8AmI0i6qAXygB8soAjAIZkicAK4BbPhDJ1BZJlDGTpHGgCg1AYxSkooxCiLxkqDNjyEoAcgFWANPyFEAjA7kKGqjQhwAKKxZWUJR6BnAAlFhqUDGhJnQA9AnBcNpkZBCawAA2ICk40hkAJlACxLwWzOSUVA4WCiw1HGrsakA

@jack-williams
Copy link
Collaborator

This was discussed when the feature was implemented: link

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Nov 8, 2019
@typescript-bot
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants