See the code below, I'm selecting one of two functions to run from dictionary
based on the type
property in the input
object.
However, because the result is a union function, Typescript throws an error.
Does anybody know how to work around this?
type typeA = {
type: A
}
type typeB = {
type: B
}
const dictionary =
{
A: function fA (input: typeA)...
B: function fB (input: typeB)...
}
const input : typeA | typeB = XYZ;
const pickedFunc = dictionary[input.type]; // input.type === 'A' | 'B' (literal union)
pickedFunc(input) // <--- Error! "typeA | typeB" is not compatible with function of "typeof fA | typeof fB"
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…