Typescript类型体操 - String to Union
作者:互联网
题目
中文
实现一个将接收到的 String 参数转换为一个字母 Union 的类型。
例如
type Test = '123';
type Result = StringToUnion<Test>; // expected to be "1" | "2" | "3"
English
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"
答案
type StringToUnion<T extends string> = T extends `${infer L}${infer R}`
? L | StringToUnion<R>
: never;
标签:Typescript,String,Union,Test,StringToUnion,type,infer 来源: https://www.cnblogs.com/laggage/p/type-challenge-string-to-union.html