Assuming strictNullChecks
are enabled, why does the third example presented below result in an error?
declare const first: undefined | number
const firstNumber: number = first === undefined ? 4 : first
declare const second: { value: undefined } | { value: number }
const secondNumber: number = second.value === undefined ? 4 : second.value
declare const third: { type: undefined, value: undefined } | { type: number, value: number }
const thirdNumber: number = third.type === undefined ? 4 : third.value
If I modify the third example as follows:
declare const third: { type: undefined, value: undefined } | { type: 'x', value: number }
const thirdNumber: number = third.type === undefined ? 4 : third.value
by changing the discriminated union to be between 'x' | undefined
instead of number | undefined
, everything seems to work correctly.