Skip to content

Instantly share code, notes, and snippets.

@Oaphi
Created October 2, 2021 04:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Oaphi/72e51a7a892441f7e1ac6e7e54c3b31a to your computer and use it in GitHub Desktop.
Save Oaphi/72e51a7a892441f7e1ac6e7e54c3b31a to your computer and use it in GitHub Desktop.
Print string union
type PrintStringUnion<T extends string, S extends string = "|", A extends string = ""> = {
[P in T]: [T] extends [P] ? `${A}${P}` : PrintStringUnion<Exclude<T, P>, S, `${A}${P}${S}`>
}[T];
type TestUnion = "A" | "B" | "C" | "D" | "E";
type U = PrintStringUnion<TestUnion>;
const test1: U = "A|B|C|D|E"; // OK
// @ts-expect-error
const test2: U = "A|B|C";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment