在ts
中,如何将字符串数组转为联合类型,比如:
const arr = ['name', 'age', 'location', 'email'];
// 通过类型操作之后得到下面这种类型
type arrType = 'name' | 'age' | 'location' | 'email'
说明: 将字符串数组中的每一个字符串都作为联合类型中的一个类型
在ts
中,如何将字符串数组转为联合类型,比如:
const arr = ['name', 'age', 'location', 'email'];
// 通过类型操作之后得到下面这种类型
type arrType = 'name' | 'age' | 'location' | 'email'
说明: 将字符串数组中的每一个字符串都作为联合类型中的一个类型
type arr = ('name'| 'age'| 'location'| 'email')[];
type UnionType<T> = T extends (infer P)[] ? P : never;
type arrType = UnionType<arr>;
这样?
13 回答12.7k 阅读
2 回答4.9k 阅读✓ 已解决
3 回答2.1k 阅读✓ 已解决
8 回答2.2k 阅读
5 回答664 阅读
3 回答2.1k 阅读
3 回答1.2k 阅读✓ 已解决