Issue
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)?
Solution
With Matt McCutchen's answer, and the recursive conditional types
since v4.1:
type UnionToParm<U> = U extends any ? (k: U) => void : never;
type UnionToSect<U> = UnionToParm<U> extends ((k: infer I) => void) ? I : never;
type ExtractParm<F> = F extends { (a: infer A): void } ? A : never;
type SpliceOne<Union> = Exclude<Union, ExtractOne<Union>>;
type ExtractOne<Union> = ExtractParm<UnionToSect<UnionToParm<Union>>>;
type ToTuple<Union> = ToTupleRec<Union, []>;
type ToTupleRec<Union, Rslt extends any[]> =
SpliceOne<Union> extends never ? [ExtractOne<Union>, ...Rslt]
: ToTupleRec<SpliceOne<Union>, [ExtractOne<Union>, ...Rslt]>
;
type test = ToTuple<5 | 6 | "l">;
Answered By - SE12938683
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.