admin管理员组文章数量:1130741
Im getting this compilation error in my Angular 2 app:
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!
Im getting this compilation error in my Angular 2 app:
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!
Share Improve this question edited Oct 14, 2021 at 9:31 Erik Philips 54.6k11 gold badges131 silver badges155 bronze badges asked Nov 1, 2016 at 10:40 Ole SpaarmannOle Spaarmann 16.7k28 gold badges104 silver badges167 bronze badges 2 |6 Answers
Reset to default 168If 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:
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:
{
...
"suppressImplicitAnyIndexErrors": true,
...
}
to the tsconfig.json
file.
Deprecation Notice for configurations in TypeScript v5.5.
These configurations will continue to "work" until TypeScript 5.5, at which point they will be removed entirely. In TypeScript 4.9.5+, 5.0, 5.1, 5.2, 5.3, and 5.4, you can specify ignoreDeprecations: "5.0" to silence this warning.
I used this to get around it so I could use the window object.
//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 (i.e. myObj[myKey] =
). To resolve it, I simply used as 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).
P.S.: another way to get the property's value is shown on a closed Typescript's issue on Github through extends:
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
compilerOptions:{
"suppressImplicitAnyIndexErrors": true,
"strictNullChecks":false,
"strictPropertyInitialization": false,
}
This works for me, in tsconfig.json:
compilerOptions:{ ... "noImplicitAny": false, ... }
本文标签:
版权声明:本文标题:javascript - TypeScript TS7015: Element implicitly has an 'any' type because index expression is not of type &am 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736758197a1951399.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
{[key: string]: any}
– Harry Ninh Commented Nov 1, 2016 at 10:43TS2339: Property 'size' does not exist on type '{ [key: string]: any; }'.
when trying to get the number of elements withthis.availableStates.size
. – Ole Spaarmann Commented Nov 1, 2016 at 10:47