美文网首页
Vue&&TypeScript--Element implici

Vue&&TypeScript--Element implici

作者: 二营长家的张大炮 | 来源:发表于2020-01-09 10:51 被阅读0次

起因

Element implicitly has an 'any' type because expression of type 'string' can't be used to index

项目用了TypeScript,
具体场景是:调用封装好的请求对象,根据传参不同调用不同的请求方法,然后代码就报错了。。。


image.png

这里使用了es6的语法,method变量是根据不同传参初始化的方法名。

解决

在找了很多资料之后发现了一篇文章:
https://stackoverflow.com/questions/57086672/element-implicitly-has-an-any-type-because-expression-of-type-string-cant-b

里面有个回答是:

This happens because you try to access plotOptions property using string name. TypeScript understands that name may have any value, not only property name from plotOptions. So TypeScript requires to add index signature to plotOptions, so it knows that you can use any property name in plotOptions. But I suggest to change type of name, so it can only be one of plotOptions properties.

这是因为您试图使用字符串名访问plotOptions属性。TypeScript知道名字可以有任何值,不仅仅是plotOptions的属性名。因此TypeScript需要向plotOptions添加索引签名,这样它就知道你可以在plotOptions中使用任何属性名。但我建议更改名称的类型,这样它就只能是plotOptions属性之一。

所以我们在项目中添加一个索引:

相关文章

网友评论

      本文标题:Vue&&TypeScript--Element implici

      本文链接:https://www.haomeiwen.com/subject/gscyactx.html