【发布时间】:2021-03-02 01:53:52
【问题描述】:
假设我有这个字符串数组:
const claims = ['val1','val2','val3','val4', ...]
如何根据claims 中的值构建接口,如下所示:
interface Claims {
val1: boolean
val2: boolean
val3: boolean
val4: boolean
...
}
我正在使用 Typescript 3.7.x
我尝试过使用这个答案:https://stackoverflow.com/a/46344193/1439748
interface Claims{ [key in claims[number]]?: boolean }
但收到这些错误:
A computed property name in an interface must refer to an expression whose type is a literal type or a 'unique symbol' type.ts(1169)
A computed property name must be of type 'string', 'number', 'symbol', or 'any'.ts(2464)
The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter.ts(2361)
'number' only refers to a type, but is being used as a value here.ts(2693)
【问题讨论】:
-
可以分享游乐场的复制链接吗?