node.jstypescripttypescript2.2

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


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
}

Solution

  • 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];