Element implicitly has an 'any' type because index expression is not of type 'number' [7015]

element implicitly has an 'any' type because type has no index signature
element implicitly has an any type because type readonly state has no index signature
suppressimplicitanyindexerrors
binding element implicitly has an 'any' type
expression of type 'string' can't be used to index type
ts7017
typescript object key implicit any
disable ts(7053)

I've taken the code from David Walsh's css animation callback and modified it to TypeScript. However, I'm getting an error and I don't know why:

interface IBrowserPrefix {
  [key: string]: string;
}

// http://davidwalsh.name/css-animation-callback
function whichAnimationEvent() {
  let x: keyof IBrowserPrefix;
  const el = document.createElement('temp');
  const browserPrefix: IBrowserPrefix = {
    animation: 'animationend',
    OAnimation: 'oAnimationEnd',
    MozAnimation: 'animationend',
    WebkitAnimation: 'webkitAnimationEnd',
  };

  for (x in browserPrefix) {
    if (el.style[x] !== undefined) {
    //           ^---- [TS Error]: Element has 'any' type b/c index expression is not of type 'number'
      return browserPrefix[x];
    }
  }
}

This is happening because you're attempting to index an object with a numeric index signature with string keys.

for x in browserPrefix will give you back a set of keys, which are strings. However for some reason CSSStyleDeclaration has its index type set to number (and not string) - see https://github.com/Microsoft/TypeScript/issues/17827.

You're getting this error because you have --noImplicitAny turned on. A way to get this working (a hacky way) would be to cast the indexer to a string:

  for (x in browserPrefix) {
    if (el.style[x as any] !== undefined) {
      return browserPrefix[x];
    }
  }

The other way would be to modify the typings (try bumping the issue on github).

while we're here, you should mark x with const and if you're going to use for-in on an object you should make sure that the property belongs to the object to avoid pulling in anything that is inherited in the prototype chain:

  for (const x in browserPrefix) {
    if (browserPrefix.hasOwnProperty(x) && el.style[x as any] !== undefined) {
      return browserPrefix[x];
    }
  }

Alternatively, use for-of with Object.keys instead of for-in.

There's no need to define x ahead of time here.

Element implicitly has an 'any' type because type 'Window' has no , The global window variable is of type Window . The type Window has no index signature, hence, typescript cannot infer the type of  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more TypeScript - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type

Try for (x of Object.keys(browserPrefix)) instead of for (x in browserPrefix).

It's typically frowned upon to use the in keyword for a loop, because you may get properties that do not belong to the object.

TypeScript : Element implicitly has an 'any' type because index , TypeScript : Element implicitly has an 'any' type because index expression is not of type 'number'. I got problem when access by key to variable  Element implicitly has an 'any' type because expression of type 'string' can't be used to index type #35859 ajhadi opened this issue Dec 26, 2019 · 4 comments Labels

There are several problems in the code, the first one is that IBrowserPrefix is defined as having a string index and thus keyof IBrowserPrefix; will actually be string. I would remove the interface and just use let x: keyof typeof browserPrefix;

The next problem is the way typescript defined the CSSStyleDeclaration interface. It only include standard properties, not vendor specific ones.

You could a type assertion to tell the compiler you know what you are doing and ignore the error

export function whichAnimationEvent() {

    const el = document.createElement('temp');
    const browserPrefix = {
        animation: 'animationend',
        OAnimation: 'oAnimationEnd',
        MozAnimation: 'animationend',
        WebkitAnimation: 'webkitAnimationEnd',
    };
    let x: keyof typeof browserPrefix;
    for (x in browserPrefix) {
        if (el.style[x as keyof CSSStyleDeclaration] !== undefined) {
            return browserPrefix[x];
        }
    }
}

You could also extend with CSSStyleDeclaration with the vendor specific keys you require.

Element implicitly has an 'any' type because expression of type , Element implicitly has an 'any' type because expression of type 'string' can't be used to index type #35859. Closed. ajhadi opened this issue on  Find an answer to your question TS7031: Binding element 'Component' implicitly has an 'any' type.

Element implicitly has an 'any' type because index expression is not , TypeScript Version: 2.7.2 Search Terms: String literal types, string enum types Numeric enum asygnable to varible type string --noImplicitAny:  error TS7031: Binding element 'any' implicitly has an 'any' type. mhegazy commented on Jul 12, 2016 this is not a type annotation, this is a rename for the destructured propeortes.

error TS7053: Element implicitly has an 'any' type because , error TS7053: Element implicitly has an 'any' type because expression of type '"A"​' can't be used to index type #TypeScript. TypeScript. 1. TypeScript : Element implicitly has an ‘any’ type because index expression is not of type ‘number’. I got problem when access by key to variable :. Sign in

TypeScript implicit 'any' type with computed property key, Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ keyword: string; hex: string; rgb: string; }'. Error TS7017 Element implicitly has an 'any' type because type 'Icons' has no index signature. ode_modules\angular2-notifications\src\simple-notifications\services otifications.service.ts 51 Please support noImplicitAny ts setting !

Comments
  • el.style is an object, not an array. For example, document.body.style.color. OP is referencing it as document.body.style['color'], which is valid.
  • @CharlesStover yes, it is. However, the TypeScript typings have the index set to a number, not a string - Hence the issue. I forgot to remove the array comment from my post, thanks for pointing it out