enum A {
FOO = 0,
BAR = 1,
BAZ = 2
}
type B = {
[key in A]: number
}
declare let b: B;
很好用。我可以用
b[A.FOO]
b[123]
. 但是,我也希望能够使用
b.length
,但当我向类型添加属性时
B
type B = {
length: number,
[key in A]: number
}
…我得到以下错误
A computed property name in a type literal must refer to an expression whose type is a literal type or a 'unique symbol' type.ts(1170)
A computed property name must be of type 'string', 'number', 'symbol', or 'any'.ts(2464)