简体   繁体   English

TypeScript TS7015:元素隐式具有“任何”类型,因为索引表达式不是“数字”类型

[英]TypeScript TS7015: Element implicitly has an 'any' type because index expression is not of type 'number'

Im getting this compilation error in my Angular 2 app:我在我的 Angular 2 应用程序中收到此编译错误:

TS7015: Element implicitly has an 'any' type because index expression is not of type 'number'.

The piece of code causing it is:导致它的一段代码是:

getApplicationCount(state:string) {
    return this.applicationsByState[state] ? this.applicationsByState[state].length : 0;
  }

This however doesn't cause this error:但是,这不会导致此错误:

getApplicationCount(state:string) {
    return this.applicationsByState[<any>state] ? this.applicationsByState[<any>state].length : 0;
  }

This doesn't make any sense to me.这对我来说没有任何意义。 I would like to solve it when defining the attributes the first time.我想在第一次定义属性时解决它。 At the moment I'm writing:目前我正在写:

private applicationsByState: Array<any> = [];

But someone mentioned that the problem is trying to use a string type as index in an array and that I should use a map.但是有人提到问题是尝试使用字符串类型作为数组中的索引,而我应该使用映射。 But I'm not sure how to do that.但我不知道该怎么做。

Thans for your help!谢谢你的帮助!

If you want a key/value data structure then don't use an array.如果你想要一个键/值数据结构,那么不要使用数组。

You can use a regular object:您可以使用常规对象:

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

getApplicationCount(state: string) {
    return this.applicationsByState[state] ? this.applicationsByState[state].length : 0;
}

Or you can use a Map :或者您可以使用Map

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

getApplicationCount(state: string) {
    return this.applicationsByState.has(state) ? this.applicationsByState.get(state).length : 0;
}

Not the OP's direct issue but for users encountering this error for libraries not under their control, one can suppress this error is by adding:不是 OP 的直接问题,但对于在不受其控制的库中遇到此错误的用户,可以通过添加以下内容来抑制此错误:

{
  ...
  "suppressImplicitAnyIndexErrors": true,
  ...
}

to the tsconfig.json file.tsconfig.json文件。

I used this to get around it so I could use the window object.我用它来解决它,所以我可以使用 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

I was actually working with React and I got this error when I assigned an object's property through a custom key (ie myObj[myKey] = ).我实际上正在使用React ,当我通过自定义键(即myObj[myKey] = )分配对象的属性时出现此错误。 To resolve it, I simply used as keyof :为了解决它,我只是用作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!';

This explicitly tells Typescript that your custom string ( myKey ) belongs to the group of properties from an interface/type you used for declaring your object ( myObj ).这明确告诉 Typescript 您的自定义字符串 ( myKey ) 属于您用于声明对象 ( myObj ) 的接口/类型的一组属性。

PS: another way to get the property's value is shown on a closed Typescript's issue on Github through extends : 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));

In tsconfig.json在 tsconfig.json 中

 compilerOptions:{

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

 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 元素隐式具有“任何”类型,因为索引表达式不是“数字”类型。ts(7015) - Element implicitly has an 'any' type because index expression is not of type 'number'.ts(7015) document.form 中的 TS(7015):“元素隐式具有‘任意’类型,因为索引表达式不是“数字”类型 - TS(7015) in a document.form : "Element implicitly has an 'any' type because index expression is not of type "number" 元素隐式具有“任何”类型,因为索引表达式不是“数字”类型[7015] - Element implicitly has an 'any' type because index expression is not of type 'number' [7015] 隐式具有“任何”类型,因为索引表达式不是“数字”类型。ts(7015) - Implicitly has an `any` type becoz index expression is not of type 'number'.ts(7015) TypeScript - ts(7053):元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引 - TypeScript - ts(7053) : Element implicitly has an 'any' type because expression of type 'string' can't be used to index 元素隐式地具有“ any”类型,因为索引表达式不是“ number”类型 - Element implicitly has an 'any' type because index expression is not of type 'number' 如何访问typescript中的Enum? 给出错误“元素隐式具有任何类型,因为索引表达式不是数字类型” - How to access Enum in typescript ? giving error "Element implicitly has an any type because index expression is not of type number" Typescript 元素隐式具有“任何”类型,因为“数字”类型的表达式不能用于索引 - Typescript Element implicitly has an 'any' type because expression of type 'number' can't be used to index 如何在 Typescript 中循环 object? 元素隐式具有“任何”类型,因为索引表达式不是“数字”类型 - How to loop an object in Typescript? Element implicitly has an 'any' type because index expression is not of type 'number' TypeScript 错误:“元素隐式具有 'any' 类型,因为类型 'any' 的表达式不能用于索引类型 - TypeScript Err: "Element implicitly has an 'any' type because expression of type 'any' can't be used to index type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM