Let's say that I have following type:
type ArrayType = ["AB"] | ["AB", "CD"] | ["X", "Y", "Z"];
I would like to transform it to the following type:
type TransformedArrayType = "AB" | "ABCD" | "XYZ";
How can I accomplish that in the Typescript?
CodePudding user response:
You can join strings recursively using template literal types:
type ArrayType = ["AB"] | ["AB", "CD"] | ["X", "Y", "Z"];
type JoinTuple<T> = T extends [infer Head, ...infer Rest]
? `${Head & string}${JoinTuple<Rest>}` : ''
type Test = JoinTuple<["AB", "CD"]> // "ABCD"
type TransformedArrayType = JoinTuple<ArrayType> // "AB" | "ABCD" | "XYZ"