String to Union
约 123 字小于 1 分钟
2026-02-11
题目
实现一个将接收到的String参数转换为一个字母Union的类型。
例如
type Test = '123';
type Result = StringToUnion<Test>; // expected to be "1" | "2" | "3"解题思路
待补充
答案
type StringToUnion<T extends string> = any验证
import type { 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'>>,
]参考
无
