3 ms·
Pretty sure TypeScript discriminated unions don't work unless you have a type field you can switch on.
by eatonphil 5y ago
Pretty sure TypeScript discriminated unions don't work unless you have a type field you can switch on.
- hyuuu 5y agowhy cant you have: type Message { contentA?: ContentAStructure, contentB?: ContentBStructure, } on read, you are already accessing the property data as typed right?
- matt_kantor 5y agoThat's not a union. Both `{}` and `{ contentA: a, contentB: b }` inhabit that type, so it doesn't represent "a value of type `Message` is either a value of `ContentAStructure` or a value of `ContentBStructure`".
- eatonphil 5y agoYou can but that's not type safe in TypeScript.