Open
Description
TypeScript Version: 3.6.0-dev.20190713
Search Terms: TS2339
, union type object
, union type property does not exist
And github suggestions while I was writing the title.
Code
type Test = {
someKey: false
} | {
someKey: {
someKeyInside?: number
},
someKeyThatExistOnlyIfOtherKeyIsNotFalse: string;
};
declare const test: Test;
if (typeof test.someKey === 'object') {
console.log(test.someKeyThatExistOnlyIfOtherKeyIsNotFalse);
}
Expected behavior:
String in someKeyThatExistOnlyIfOtherKeyIsNotFalse
should be logged
Actual behavior:
error TS2339: Property 'someKeyThatExistOnlyIfSomeKeyIsNotFalse' does not exist on type 'Test2'.
Property 'someKeyThatExistOnlyIfSomeKeyIsNotFalse' does not exist on type '{ someKey: false; }'.
29 console.log(test2.someKeyThatExistOnlyIfSomeKeyIsNotFalse); // TS2339: Property 'someKeyThatExistOnlyIfSomeKeyIsNotFalse' does not exist on type '{ someKey: false; }'.
Take a look at type Test1
. The only one difference from type Test2
is someKeyInside
should be always defined.