相关疑难解决方法(0)

TypeScript TS7015:Element隐式具有"any"类型,因为索引表达式不是"number"类型

我在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)

但有人提到问题是试图在数组中使用字符串类型作为索引,我应该使用映射.但我不知道该怎么做.

有你的帮助!

javascript typescript angular

25
推荐指数
5
解决办法
3万
查看次数

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

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)

typescript reactjs

6
推荐指数
13
解决办法
4917
查看次数

TypeScript-元素隐式地具有“ any”类型,因为类型“ string”的表达式不能用于索引类型

假设我有这个:

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]

javascript typescript

3
推荐指数
4
解决办法
1402
查看次数

标签 统计

typescript ×3

javascript ×2

angular ×1

reactjs ×1