I would like to have a type that ensures an object has a type of A
or B
or A and B
. However one of the cases that I think should fail is not. I'm sure it's something stupid I just can't see it yet.
interface ValueSelector
{
type: "id" | "value_string"
value: string
}
interface TemporalSelector
{
id: number
}
type Selector = (ValueSelector & TemporalSelector) | ValueSelector | TemporalSelector
// Should error
const e0: Selector = {}
const e1: Selector = { id: 0, value: "" } // <-- does not error
const e2: Selector = { type: "id" }
const e3: Selector = { type: "value_string" }
const e4: Selector = { value: "" }
const e5: Selector = { value: "" }
// Should pass
const a1: Selector = { id: 0 }
const a2: Selector = { type: "id", value: "" }
const a3: Selector = { type: "value_string", value: "" }
const a4: Selector = { id: 0, type: "id", value: "" }
const a5: Selector = { id: 0, type: "value_string", value: "" }