Decompose a TypeScript union type into specific types
Asked Answered
F

2

7

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)?

Footnote answered 22/10, 2018 at 13:54 Comment(1)
You can turn the union to an intersection.. #50375408 and conditional types do distribute over unions so you can turn a union into a union of arrays of each member for example.But your specific example of tuning the union to a tuple I don't think is possible, at least not for an arbitrary number of tuple members (we might get it to work for a specific number of members but I have a feling we'd end up with something like [number, string] | [string, number])Nicolnicola
M
12

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">;
Maure answered 22/11, 2021 at 5:33 Comment(2)
Awesome, but can anyone explain me why a type which should return the inferred parameter type I actually returns an intersection of the original function types? type t = ((k: 5) => void) & ((k: 6) => void) & ((k: "l") => void). First of all why is the result an intersection? Which typescript rule applies here? And second, I would have expected something like 5 | 6 | 'l' to be returned. Looks like weird black magic to me. Can you tell me the rules which apply here and make it work?Anatomist
@Anatomist Which type is the said "a type which..."?Maure
V
9

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.

// https://stackoverflow.com/a/50375286
type UnionToIntersection<U> = 
  (U extends any ? (k: U)=>void : never) extends ((k: infer I)=>void) ? I : never

type UnionToFunctions<U> =
    U extends unknown ? (k: U) => void : never;

type IntersectionOfFunctionsToType<F> =
    F extends { (a: infer A): void; (b: infer B): void; (c: infer C): void; } ? [A, B, C] :
    F extends { (a: infer A): void; (b: infer B): void; } ? [A, B] :
    F extends { (a: infer A): void } ? [A] :
    never;

type SplitType<T> =
    IntersectionOfFunctionsToType<UnionToIntersection<UnionToFunctions<T>>>;

type Test1 = SplitType<number>;                    // [number]
type Test2 = SplitType<number | string>;           // [string, number]
type Test3 = SplitType<number | string | symbol>;  // [string, number, symbol]
Villeinage answered 22/10, 2018 at 15:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.