![](/img/trans.png)
[英]TypeScript - A computed property name must be of type 'string', 'number', 'symbol', or 'any'
[英]Typescript - An index expression argument must be of type 'string', 'number', 'symbol' or 'any'
我使用的是Typescript 1.7.5,我在以下情况下遇到了An index expression argument must be of type 'string', 'number', or '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();
});
它是导致错误的channel
变量吗? 如何输入并同时对其进行解构?
PS我省略了不必要的代码,所以如果事情没有意义,请告诉我。
似乎TypeScript无法正确猜测类型,因此我们可以使用显式类型声明来帮助它:
// .forEach( ([channel, color]) => {
.forEach( ([channel, color]: [string, string]) => {
甚至,如果颜色的类型更具体,例如:
const colors: any [] = ...
应该有助于确保索引/键通道和支持类型的颜色
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.