I actually don't remember the historical reasons for this is the case, but have the rule memorized...
In general, TypeScript won't let you access properties of a value that aren't defined in all possible types the value might be. So you can't do things like:
declareconstvalue: {a: string}|{b: string};value.a;// ~// Property 'a' does not exist on type '{ a: string; } | { b: string; }'.// Property 'a' does not exist on type '{ b: string; }'.However, you can do an existence check with the in operator. That will work as a form of type narrowing:
declareconstvalue: {a: string}|{b: string};if('a'invalue){value.a;// Ok// ^? { a: string }}https://discord.com/channels/508357248330760243/942074070860705852/1008521509532356649
I actually don't remember the historical reasons for this is the case, but have the rule memorized...
In general, TypeScript won't let you access properties of a value that aren't defined in all possible types the value might be. So you can't do things like:
However, you can do an existence check with the in operator. That will work as a form of type narrowing:
https://discord.com/channels/508357248330760243/942074070860705852/1008521509532356649