Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
const list = ['aa', 'bb', 'cc']; type obj = { aa: any; bb: any; cc: any}
我们有清单。以及如何在打字稿中获取类型 obj
假设你list as const这样声明,
list
as const
const list = ["aa", "bb", "cc"] as const;
或者使用这种语法,
const list = <const>["aa", "bb", "cc"];
你可以定义这样的类型:
type Obj = { [K in typeof list[number]]: any };
如果list未声明as const,恐怕这是不可能的,因为您无法访问类型定义中的实际数组值。