You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
leta: string|undefined;typeB_1={a: string,c: 'test',d: 'yeah'};typeB_2={c: 'test'}typeB=B_1|B_2;// this should complain that// - property a should not not exist// - or property a should be string and property d is missingconstb: B={ a,c: 'test'};if('a'inb){// because of this issue, b is narrowed to B_1 here, while a is undefined and d is not presentconsole.log(b);}
π Actual behavior
const b: B = { a, c: 'test'}; does not throw a TS error.
π Expected behavior
const b: B = { a, c: 'test'}; throws a TS error.
The text was updated successfully, but these errors were encountered:
The assignment is allowed, because { a, c: 'test' } is a valid B (specifically, it's a valid B_2). Excess property checks is not performed for union types, see #51873.
This unfortunately leads to an intentional unsoundness using the in operator, see #34975.
Thanks for your quick reply! I think this issue can then be closed as it doesn't add much to the two issues you mentioned. Sorry for wasting your time.
Bug Report
π Search Terms
union undefined incorrectly narrowed
π Version & Regression Information
This is the behavior in every version I tried (back to 3.3.3), and I reviewed the FAQ for entries about bugs, undefined and unions.
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
const b: B = { a, c: 'test'};
does not throw a TS error.π Expected behavior
const b: B = { a, c: 'test'};
throws a TS error.The text was updated successfully, but these errors were encountered: