typescript - 打字稿错误:TS7053 元素隐式具有“任何”类型
问题描述
这是我的代码的一部分:
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} = {}
推荐阅读
- c# - httpClient.GetStringAsync 返回 403,但我尝试使用的路线在邮递员中工作
- c# - 在输入方法之前找不到文件或其依赖项之一
- r - 使用 broom::tidy 为 GAM 系数和置信区间取幂
- excel - SAP Webi 文档转 Excel,其他值没有小数位
- discord.py - 如何从错误函数中重置命令冷却时间
- javascript - 在 Javascript 中,我的文本字段无法识别名称中的特殊符号(é 字符)
- arrays - 将数组的内容打印为字符串
- powershell - 为什么通过 Dropbox API 查看的两个文件具有相同的 id?
- macos - macOS:确定从 iconForFile 方法返回的图标的实际大小
- excel - 使用 userfom 时间间隔数据绘制图表时遇到问题