Unsound type inference with `in` operator on union
kaya3 opened this issue · 2 comments
kaya3 commented
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.
jack-williams commented
This was discussed when the feature was implemented: link
typescript-bot commented
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.