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

Ole*_*ann 25 javascript typescript angular

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

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

有你的帮助!

Nit*_*mer 25

如果需要键/值数据结构,请不要使用数组.

您可以使用常规对象:

private applicationsByState: { [key: string]: any[] } = {};

getApplicationCount(state: string) {
    return this.applicationsByState[state] ? this.applicationsByState[state].length : 0;
}
Run Code Online (Sandbox Code Playgroud)

或者您可以使用地图:

private applicationsByState: Map<string, any[]> = new Map<string, any[]>();

getApplicationCount(state: string) {
    return this.applicationsByState.has(state) ? this.applicationsByState.get(state).length : 0;
}
Run Code Online (Sandbox Code Playgroud)


Rob*_*ita 13

不是OP的直接问题,而是对于不在其控制范围内的库遇到此错误的用户,可以通过添加以下命令来抑制此错误:

"suppressImplicitAnyIndexErrors": true,

到tsconfig.json文件。

  • 您错过了使用“不受您控制”的库的要点。我仍然坚持我原来的评论;因为主题是用户将看到和搜索的错误,无论 OP 的描述如何。 (4认同)
  • 否决票但没有解释?这在访问不受开发人员控制的字典对象时很有帮助。 (3认同)
  • OP 并没有询问不受他们控制的图书馆,并且您在回答中也没有提及。如果你说“在你的情况下你不应该这样做,但对于其他人在不受你控制的代码中出现此错误,那么你可以按如下方式抑制错误......”,那么就会有很好并得到了我的支持。 (2认同)
  • 是的,我会进行更改。感谢您指出。 (2认同)

CPH*_*hon 12

我实际上正在使用React,当我通过自定义键(即myObj[myKey] = )分配对象的属性时出现此错误。要解决这个问题,我只是用作为keyof

interface IMyObj { title: string; content: string; }
const myObj: IMyObj = { title: 'Hi', content: 'Hope all is well' };
const myKey: string = 'content';

myObj[myKey as keyof IMyObj] = 'All is great now!';
Run Code Online (Sandbox Code Playgroud)

这明确告诉 Typescript 您的自定义字符串 ( myKey ) 属于您用于声明对象 ( myObj )的接口/类型的属性组。

PS:另一种获取属性值的方法显示在 Github 上通过extends关闭的Typescript 问题

interface IMyObj {
  title: string;
  content: string;
}

const myObj: IMyObj = { title: 'Hi', content: 'Hope all is well' };
const myKey: string = 'content';

const getKeyValue = <T extends object, U extends keyof T>(obj: T) => (key: U) =>
  obj[key];
console.log(getKeyValue(myObj)(myKey));
Run Code Online (Sandbox Code Playgroud)


roh*_*095 8

在 tsconfig.json 中

 compilerOptions:{

  "suppressImplicitAnyIndexErrors": true,
  "strictNullChecks":false,
  "strictPropertyInitialization": false,

 }
Run Code Online (Sandbox Code Playgroud)

  • 仅更改“tsconfig.json”设置并不是真正的答案。也许是一条评论。可以操纵这些设置来消除任何/所有 TS 错误。 (33认同)

小智 7

我用它来解决它,所以我可以使用window对象。

//in js code somewhere
window.DataManager = "My Data Manager";


//in strict typescript file
let test = (window as { [key: string]: any })["DataManager"] as string;
console.log(test); //output= My Data Manager
Run Code Online (Sandbox Code Playgroud)

  • 永远不应该使用窗口作为解决方法。这与使用全局变量相同。非常令人沮丧的糟糕做法会导致比其解决的问题更多的长期问题。 (3认同)