I'm having trouble understanding why the code below won't compile:
type X = {
id: "primary"
inverted?: boolean
} | {
id: "secondary"
inverted?: boolean
} | {
id: "tertiary"
inverted?: false
}
const a = true
const x: X = {
id: a ? "primary" : "secondary"
}
x should be valid for any scenario.
But there's an error in compilation:
test.ts:14:7 - error TS2322: Type '{ id: "primary" | "secondary"; }' is not assignable to type 'X'.
Type '{ id: "primary" | "secondary"; }' is not assignable to type '{ id: "tertiary"; inverted?: false; }'.
Types of property 'id' are incompatible.
Type '"primary" | "secondary"' is not assignable to type '"tertiary"'.
Type '"primary"' is not assignable to type '"tertiary"'.