这是我的代码的一部分:
const myObj: object = {}
const propname = 'propname'
myObj[propname] = 'string'
但是我得到了错误:
ERROR in path/to/file.ts(4,1)
TS7053: Element implicitly has an 'any' type because expression of type '"propname"' can't be used to index type '{}'.
Property 'propname' does not exist on type '{}'.
这有什么问题,我该如何解决?
您必须定义对象具有哪种索引类型。在您的情况下,它是一个string
基于索引的索引。
const myObj: {[index: string]:any} = {}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句