在打字稿中获取数字对象属性的类型作为字符串?

Sam*_*Sam 2 types typescript

有一个与此类似的对象:

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"才能自动完成?

axt*_*tck 5

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)