discriminated union
Code
type T1 = {
as?: 'button',
onClick?: (el: number) => void
}
type T2 = {
as: 'a',
onClick?: (el: string) => void
}
type T = T1 | T2;
// onClick first parameter type is inferred according to T1 (number)
const myT1_1: T = {
as: undefined,
onClick: _ev => {}
}
// onClick first parameter type is inferred according to T1 (number)
const myT1_2: T = {
as: 'button',
onClick: _ev => {}
}
// onClick first parameter type is inferred according to T2 (string)
const myT2_1: T = {
as: 'a',
onClick: _ev => {}
}
// onClick first parameter type is not inferred (any)
const myT1_3: T = {
onClick: _ev => {}
}
Expected behavior:
onClick _ev parameter of myT1_3 is inferred to be a number
Actual behavior:
onClick _ev parameter of myT1_3 is any
Playground Link:
Playground Link
If the callback in T1 and T2 is replaced with a primitive member then myT1_3 is inferred correctly. Playground Link
discriminated union
Code
Expected behavior:
onClick
_evparameter ofmyT1_3is inferred to be anumberActual behavior:
onClick
_evparameter ofmyT1_3isanyPlayground Link:
Playground Link
If the callback in
T1andT2is replaced with a primitive member thenmyT1_3is inferred correctly. Playground Link