TypeScript seems to have problems to infer union types of type guards. As an example, consider a function to combine an array of type guards with the following signature
function combine<T>(guards: ((x: any) => x is T)[]): (x: any) => x is T
and consider the following type guards with A
and B
having different properties
function isA(x: any): x is A
function isB(x: any): x is B
Now I would expect combine([isA, isB])
to work and have the inferred type (x: any) => x is A | B
but instead I get an error saying that an argument of type((x: any) => x is A | (x: any) => x is B)[]
is not assignable to parameter of type (x: any) => x is A
, meaning that T
is inferred as A
rather than A | B
.
When specifying T
explicitely, i.e. combine<A|B>([isA, isB])
, it works as expected. Is there a way to change the signature of combine
such that this could be inferred?
function combine2<T, U>(guards: [((x: any) => x is T), ((x: any) => x is U)]): (x: any) => x is T | U
, but I guess you want to accept an array with various amount of items? – Caramiriel