I wonder whether it's possible to “split” union types into the specific subtypes in TypeScript. This is the code I tried to use, it should be obvious what I'm trying to achieve from the snippet:
type SplitType<T> =
T extends (infer A)|(infer B)
? Something<A, B>
: T;
In this example Something<A, B>
could be [A, B]
, or a completely different type. This would mean that SplitType<string>
would just output a string
, but SplitType<number|string>
would mean [number, string]
.
Is something like that possible in TypeScript? And if not, is there a feature that will allow this in the future (eg. variadic types)?
For a fixed maximum number of union members, we can extract the union members in a single implementation-defined order by generating an intersection of call signatures and then matching it against a type with multiple call signatures. This version only works with
strictFunctionTypes
enabled.