[Typescript] 34. Medium - String to Union
typescript string to 34 union Medium
2023-09-14 08:59:12 时间
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] NoInfer
- [Typescript] Defining exclusive properties with TypeScript
- [Typescript] 108. Easy - Nullable
- [Typescript] Function scope in typescript
- [Typescript] Get class properties type in union
- [Typescript] Step 4. ESLint for Typescript
- [React Typescript 2022] Use TypeScript to Type a React Class Component
- [TypeScript] Statically Type String Literals with Template Literal Types in TypeScript
- [Typescript] Statically Type Unknown Values with TypeScript's unknown Type
- [Typescript] Simplify iteration of custom data structures in TypeScript with iterators (backwards iteration with for ... of.. loop)
- [Typescript] Make TypeScript Class Usage Safer with Strict Property Initialization
- [Typescript] Improve Readability with TypeScript Numeric Separators when working with Large Numbers
- [Typescript] Generics using TypeScript
- [TypeScript] Typescript Interfaces vs Aliases Union & Intersection Types
- [Typescript 4.9] TypeScript 4.9: satisfies operator
- [Typescript] 109. Hard - Length of String 2
- [Typescript] 32. Medium - Permutation
- [Typescript] ts-expect-error
- [Typescript] Making TypeScript Stick - 5 - Extends
- [TypeScript] Make typescript stick - 2
- [React Typescript 2022] Refactor a React Component using TypeScript
- [TypeScript] String Enums and Inlining Members
- [Typescript] Exclude Properties from a Type in TypeScript (Omit)
- [Tools] Unlock TypeScript's Features in Vanilla JS with @ts-check and JSDoc
- [TypeScript] Typescript Interfaces vs Aliases Union & Intersection Types
- 【RF库测试】Encode String To Bytes&Decode Bytes To String& should be string&should be unicode string &should not be string
- 使用 TypeScript 编写 SAP UI5 应用的准备工作
- TypeScript里string和String,真不是仅仅是大小写的区别