有一个与此类似的对象:
const obj = {
1: "one",
2: "two",
3: "three",
}
type Keys = keyof typeof obj; // type of Key is 1 | 2 | 3
Run Code Online (Sandbox Code Playgroud)
我如何获得Keys类型(字符串)"1" | "2" | "3"才能自动完成?
从TypeScript 4.1开始,可以使用模板文字类型。
const obj = {
1: "one",
2: "two",
3: "three",
}
type Keys = `${keyof typeof obj}`;
const value: Keys = "1";
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
51 次 |
| 最近记录: |