打字稿错误:TS7053 元素隐式具有“任何"类型 [英] Typescript error: TS7053 Element implicitly has an 'any' type

查看:66
本文介绍了打字稿错误: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 '{}'.

这里出了什么问题,我该如何解决?

What is wrong here, and how can I fix it?

推荐答案

你必须定义对象有什么样的索引类型.在您的情况下,它是基于 string 的索引.

You have to define what kind of index type the object has. In your case it is a string based index.

const myObj: {[index: string]:any} = {}

这篇关于打字稿错误:TS7053 元素隐式具有“任何"类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆