Trying to create a type that ensures an object has a type of A
, B
, or A and B
. Despite my efforts, one of the cases I thought should fail is not failing. I suspect it's a silly mistake that I just can't seem to identify 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: "" }