Created
October 2, 2021 04:12
-
-
Save Oaphi/72e51a7a892441f7e1ac6e7e54c3b31a to your computer and use it in GitHub Desktop.
Print string union
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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