TypeScript warning => TS7017: Index signature of object type implicitly has any type

516 Views Asked by At

I am getting the following TypeScript warning -

Index signature of object type implicitly has any type

Here is the code that cases the warning:

Object.keys(events).forEach(function (k: string) {

  const ev: ISumanEvent = events[k]; // warning is for this line!!
  const toStr = String(ev);
  assert(ev.explanation.length > 20, ' => (collapsed).');

  if (toStr !== k) {
    throw new Error(' => (collapsed).');
  }
});

can anyone determine from this code block why the warning shows up? I cannot figure it out.

If it helps this is the definition for ISumanEvent:

interface ISumanEvent extends String {
  explanation: string,
  toString: TSumanToString
}
1

There are 1 best solutions below

0
On BEST ANSWER

You could add an indexer property to your interface definition:

interface ISumanEvent extends String {
  explanation: string,
  toString: TSumanToString,
  [key: string]: string|TSumanToString|ISumanEvent;
}

which will allow you to access it by index as you do: events[k];. Also with union indexer it's better to let the compiler infer the type instead of explicitly defining it:

const ev = events[k];