Go Back
Refer: Medium - String to Union
Describe
Implement the String to Union type. Type take string argument. The output should be a union of input letters
For example
code snippetCopytypescripttype Test = '123'
type Result = StringToUnion<Test> // expected to be "1" | "2" | "3"
Test Cases
code snippetCopytypescriptimport { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<StringToUnion<''>, never>>,
Expect<Equal<StringToUnion<'t'>, 't'>>,
Expect<Equal<StringToUnion<'hello'>, 'h' | 'e' | 'l' | 'l' | 'o'>>,
Expect<Equal<StringToUnion<'coronavirus'>, 'c' | 'o' | 'r' | 'o' | 'n' | 'a' | 'v' | 'i' | 'r' | 'u' | 's'>>,
]
Solution
code snippetCopytypescripttype StringToUnion<T extends string, C = never> = T extends `${infer P}${infer K}`
? K['length'] extends 0
? C | P
: StringToUnion<K, C | P>
: C