type StringToUnion<T extends string> = T extends `${infer First}${infer Rest}` ? First | StringToUnion<Rest> : never
Solution by eunsukimme #35239
type StringToUnion<T extends string> = T extends `${infer First}${infer Rest}`
? First | StringToUnion<Rest>
: never
Solution by HrOkiG2 #35215
type StringToUnion<T extends string> = T extends `${infer First}${infer Rest}` ? First | StringToUnion<Rest> : never
Solution by devshinthant #34609
type StringToUnion<T extends string> = T extends `${infer R}${infer rest}` ? R | StringToUnion<rest> : never
Solution by ouzexi #34018
by Andrey Krasovsky (@bre30kra69cs) #medium #union #string
Implement the String to Union type. Type take string argument. The output should be a union of input letters
For example
type Test = "123"
type Result = StringToUnion<Test> // expected to be "1" | "2" | "3"
View on GitHub: https://tsch.js.org/531
...
type StringToUnion<T extends string> = T extends `${infer L}${infer R}`
? L | StringToUnion<R>
: never
Solution by veralex #33787
type StringToUnion<T extends string,R extends any=never> = T extends `${infer F}${infer B}` ? StringToUnion<B,F | R> : R;
Solution by knife710 #33762
type StringToUnion<T extends string, A extends string[] = []> = T extends ${infer S}${infer E}
? StringToUnion<E, [...A, S]> : A[number]
Solution by rookiewxy #33748
// 你的答案
type Tuple2Union<T> = T extends any[] ? T[number] : never;
type StringToUnion<T extends string, U extends any[]= []> = T extends `${infer H}${infer R}` ? StringToUnion<R, [...U, H]> : Tuple2Union<U>;
Solution by HelloGGG #33415
type StringToUnion<T extends string> = T extends `${infer F}${infer L}` ? F | StringToUnion<L> : never;
Solution by ZhipengYang0605 #32831
type StringToUnion<T extends string> = T extends `${infer First}${infer Rest}`
? First | StringToUnion<Rest>
: never
Solution by ZhulinskiiDanil #32701
type StringToUnion<T extends string, R extends string[] = []> =
T extends `${infer Shift}${infer Rest}`
? StringToUnion<Rest, [Shift, ...R]>
: R[number]
Solution by dev-hobin #32414
// 解答をここに記入
type StringToUnion<T extends string, K = never> = T extends `${infer F}${infer R}`
? StringToUnion<R, K | F>
: K;
Solution by pea-sys #32195
type StringToUnion<T extends string, U extends string[] = []> = T extends ""
? never
: T extends `${infer F}${infer L}`
? L extends ""
? [...U, F][number]
: StringToUnion<L, [...U, F]>
: never;
Solution by gasmg #32146
type StrintToUnion<T extends string> = T extends `${infer Letter}${infer Rest}`
? Letter | StrintToUnion<Rest>
: never;
Solution by rkamely #32061
type StringToUnion<T extends string, K = never> = T extends `${infer F}${infer R}`
? StringToUnion<R, K | F>
: K;
Solution by jinyoung234 #31813
type StringToUnion<T extends string, R extends any[] = []> = T extends `${infer F}${infer Rest}` ? StringToUnion<Rest, [...R, F]> : R[number]
Solution by kai-phan #31647
type StringToUnion<
S extends string,
W extends any[] = []
> = S extends `${infer H}${infer T}` ? StringToUnion<T, [...W, H]> : W[number];
Solution by vipulpathak113 #31501
type StringToUnion<S extends string> =
S extends ""
? never
: S extends `${infer F}${infer Rest}`
? F | StringToUnion<Rest>
: S;
Solution by ricky-fn #31298
type StringToUnion<T extends string> = T extends `${infer F}${infer L}` ? F | StringToUnion<L> : never;
Solution by RainbowIsPerfect #31281
type StringToUnion<T extends string, R = {}> = T extends `${infer A}${infer B}` ? StringToUnion<B, {[k in keyof R | A]: true}> : keyof R;
Solution by eward957 #31249
// to array then to union
type StringToUnion<T extends string, K extends any[] = []> = T extends ``
? K[number]
: T extends `${infer F}${infer R}`
? StringToUnion<R, [...K, F]>
: never
Solution by gamejoye #30886
type StringToUnion<T extends string, U extends string[]=[]> =
T extends `${infer F}${infer E}`
? StringToUnion<E, [...U, F]>
: U[number]
Solution by neMew5 #30767
type StringToUnion<T extends string> = T extends `${infer First}${infer Other}` ? First | StringToUnion<Other> : never
Solution by MrRabbit1993 #30464
type StringToUnion<T extends string, R extends any[] = []> = T extends ''
? R[number]
: T extends `${infer F}${infer Rest}`
? StringToUnion<Rest, [...R, F]> : [];
Solution by kai-phan #30365
type StringToUnion<T extends string, K extends Array<unknown> = []> = T extends `${infer First}${infer Last}`
? First extends ''
? K
: StringToUnion<Last, [...K, First]>
: K[number]
Solution by agus-wesly #30283
type StringToUnion<T extends string> = T extends `${infer L}${infer R}` ? L | StringToUnion<R> : never;
Solution by kanishev #30131
type Test = '123';
type StringToUnion<T extends string> = T extends `${infer U}${infer R}` ? U | StringToUnion<R> : never
type Result = StringToUnion<Test>;
Solution by rxMATTEO #30098
type StringToUnion<T extends string, P = never> = T extends '' ? P : T extends `${infer X}${infer Y}` ?
StringToUnion<Y, P | X> : T
Solution by gaoerjing #30000
// your answers
type StringToUnion<T extends string, Acc extends string[] = []> = T extends `${infer F}${infer Rest}` ? StringToUnion<Rest, [ ...Acc, F]>: Acc[number]
Solution by kerolossamir165 #29973
type StringToUnion<T extends string, TRes extends string = never> =
T extends `${infer Letter}${infer Rest}`
? StringToUnion<Rest, Letter | TRes>
: TRes
Solution by gstcarv #29912