我可以将对象属性的名称用作对象中的字符串值吗? 我的意思是,我想实现这一点:
type MyObjectType = /* ... */
const myValidObject: MyObjectType = {
a: 'a',
b: 'b'
} // valid object of type MyObjectType
const myInvalidObject: MyObjectType = {
a: 'a',
b: 'c'
} // invalid object of type MyObjectType, property b should have value 'b'
我试过这样的方法,但不起作用:
type MyObjectType = {
[K: string]: `${K}`
}