我正在使用打字稿1.7.5,并且遇到以下情况,索引表达式参数必须为’string’,’number’或’any’类型的错误:
const settings: any = {};
_.forEach(data, (d, name: string) => { //data is just an object
settings[name] = {};
const colors = ColorGenerator.generateColors(Object.keys(d.ch).length);
_(d.ch)
.keys()
.zip(colors)
.forEach(([channel, color]) => {
// name and channel are both strings
settings[name][channel] = { // this line is throwing the error
channel,
color,
visible: true
};
}).value();
});
是导致错误的通道变量吗?我如何同时输入和销毁它?
附言我已省略了不必要的代码,因此,如果没有任何意义,请告诉我.
解决方法:
看来TypeScript无法正确猜测类型,因此我们可以通过显式类型声明来帮助它:
// .forEach( ([channel, color]) => {
.forEach( ([channel, color]: [string, string]) => {
甚至,如果颜色的类型会更具体,例如:
const colors: any [] = ...
应该有助于确保索引/键通道和支持的类型的颜色
原文地址:https://codeday.me/bug/20191119/2032642.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。