Dev Helpers
← Back

Distribute over a union

Distribute over a union using TypeScript.

Open this example in the interactive library

Source code

type ElementOf<T> = T extends readonly (infer U)[] ? U : never;
console.log(
  (() => {
    type ArrayOf<T> = T extends unknown ? T[] : never;
    const value: ArrayOf<string | number> = [1, 2];
    return value;
  })(),
);

Sample output

[ 1, 2 ]