[Typescript] 34. Medium - String to Union
typescript string to 34 union Medium
2023-09-14 09:00:45 时间
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"
/* _____________ Your Code Here _____________ */
type StringToUnion<T extends string> = T extends '' ? never : T extends `${infer First}${infer REST}` ? `${First}` | StringToUnion<REST>: never;
/* _____________ Test Cases _____________ */
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'>>,
]
相关文章
- [Typescript Challenges] 144 Hard - Transpose
- [Typescript] Use never for readable string
- [Typescript] Clean type
- [Typescript] TS-toolbelt Split
- [Typescript] 115. Hard - Drop String
- [Typescript] Tips: Map over a union type
- [Typescript] 34. Medium - String to Union
- [Typescript] “Partial” Mapped Type
- [Typescript] 131. Extreme - Query String Parser
- [Typescript] 115. Hard - Drop String
- [CLI] Create a Hybrid Single-Multi Command Node.js CLI with Oclif and TypeScript
- [TypeScript] Create random integers in a given range
- 巧用 TypeScript Literal Types 模拟枚举类型
- 使用npm安装TypeScript
- Typescript 严格模式有多严格?