【发布时间】:2020-11-04 09:49:21
【问题描述】:
Typescript 中有没有办法动态地使用已知的type 作为界面的键?
例如,我有这个type:
type properties = 'name' | 'age' | 'height';
我想允许一个对象使用任何/所有这些“属性”作为键,并使用一个字符串作为值:
{name: 'abc', height: '2'} 或 {height: '2'} 允许
我已经使用值而不是键来完成此操作,所以我想要与此相反:
interface person {
[key: string]: properties
}
类似这样的:
interface person {
[key: properties]: string
}
【问题讨论】:
-
type person = { [key in properties]?: string }
标签: typescript