我在Angular 2应用程序中收到此编译错误:
TS7015: Element implicitly has an 'any' type because index expression is not of type 'number'.
Run Code Online (Sandbox Code Playgroud)
导致它的代码是:
getApplicationCount(state:string) {
return this.applicationsByState[state] ? this.applicationsByState[state].length : 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,这不会导致此错误:
getApplicationCount(state:string) {
return this.applicationsByState[<any>state] ? this.applicationsByState[<any>state].length : 0;
}
Run Code Online (Sandbox Code Playgroud)
这对我没有任何意义.我想在第一次定义属性时解决它.目前我正在写:
private applicationsByState: Array<any> = [];
Run Code Online (Sandbox Code Playgroud)
但有人提到问题是试图在数组中使用字符串类型作为索引,我应该使用映射.但我不知道该怎么做.
有你的帮助!
Trying out TypeScript for a React project and I'm stuck on this error:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ train_1: boolean; train_2: boolean; train_3: boolean; train_4: boolean; }'.
No index signature with a parameter of type 'string' was found on type '{ train_1: boolean; train_2: boolean; train_3: boolean; train_4: boolean; }'
Run Code Online (Sandbox Code Playgroud)
Which appears when I try to filter the array in my component
.filter(({ name }) => plotOptions[name]); …Run Code Online (Sandbox Code Playgroud) 假设我有这个:
const color = {
red: null,
green: null,
blue: null
};
const newColor = ['red', 'green', 'blue'].filter(e => color[e]);
Run Code Online (Sandbox Code Playgroud)
错误在color[e]底部附近:
元素隐式地具有“ any”类型,因为类型“ string”的表达式不能用于索引类型“ {red:null; 绿色:null;蓝色:null;}'。在类型“ {{red:null;}”上找不到带有参数“ string”的索引签名。绿色:null;蓝色:null;}'。
我尝试在TypeScript文档上到处查找,但是我interface对此到底想怎么想,以便它可以接受color[e]?