【发布时间】:2022-01-25 21:06:18
【问题描述】:
有一个类似的对象:
const obj = {
1: "one",
2: "two",
3: "three",
}
type Keys = keyof typeof obj; // type of Key is 1 | 2 | 3
我如何让Keys 成为(字符串)"1" | "2" | "3" 类型以便自动完成?
【问题讨论】:
标签: typescript types
有一个类似的对象:
const obj = {
1: "one",
2: "two",
3: "three",
}
type Keys = keyof typeof obj; // type of Key is 1 | 2 | 3
我如何让Keys 成为(字符串)"1" | "2" | "3" 类型以便自动完成?
【问题讨论】:
标签: typescript types
由于TypeScript 4.1,可以使用Template Literal Types。
const obj = {
1: "one",
2: "two",
3: "three",
}
type Keys = `${keyof typeof obj}`;
const value: Keys = "1";
【讨论】: