I have several similar types like { value: number, type: 'number' }
and I was going to put them in union types such as:
type UnionType1 = { value: number, type: 'number' } | { value: string, type: 'string' }
type Pass1 = UnionType1 extends { value: any, type: any } ? true : false
// ^? type Pass1 = true
However, if one of the union types has an optinal field then the condition check would fail:
type UnionType2 = { value: number, type: 'number' } | { value?: string, type: 'string' }
type Pass2 = UnionType2 extends { value: any, type: any } ? true : false
// ^? type Pass2 = false
I want the Pass2
also to be true
, without changing the UnionType2, if but I don't know how.
You can simply make the value
property optional too:
type UnionType2 = { value: number, type: 'number' } | { value?: string, type: 'string' }
type Pass2 = UnionType2 extends { value?: any, type: any } ? true : false