close

[Fixed] TypeScript TS7015: Element implicitly has an ‘any’ type because index expression is not of type ‘number’

Today We are Going To Solve TypeScript TS7015: Element implicitly has an ‘any’ type because index expression is not of type ‘number’ in Javascript. Here we will Discuss All Possible Solutions and How this error Occurs So let’s get started with this Article.

How to Fix TypeScript TS7015: Element implicitly has an ‘any’ type because index expression is not of type ‘number’ Error?

  1. How to Fix TypeScript TS7015: Element implicitly has an 'any' type because index expression is not of type 'number' Error?

    To Fix TypeScript TS7015: Element implicitly has an 'any' type because index expression is not of type 'number' Error just Use a regular object. Just use a regular object just like below: private applicationsByState: { [key: string]: any[] } = {}; getApplicationCount(state: string) { return this.applicationsByState[state] ? this.applicationsByState[state].length : 0; }

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

    To Fix TypeScript TS7015: Element implicitly has an 'any' type because index expression is not of type 'number' Error just Add this to the tsconfig.json file. Just add this to the tsconfig.json file to solve your error. { ... "suppressImplicitAnyIndexErrors": true, ... }

Solution 1 : Use a regular object

Just use a regular object just like below:

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

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

Solution 2 : Add this to the tsconfig.json file

Just add this to the tsconfig.json file to solve your error.

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

Solution 3 : Use the window object.

Here you have to use the window object.

window.DataManager = "My Data Manager";
let test = (window as { [key: string]: any })["DataManager"] as string;
console.log(test); //output= My Data Manager

Conclusion

So these were all possible solutions to this error. I hope your error has been solved by this article. In the comments, tell us which solution worked? If you liked our article, please share it on your social media and comment on your suggestions. Thank you.

Also Read These Solutions

Leave a Comment