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)?
[number, string] | [string, number]
) – Nicolnicola