What it means is that the signature of an index of an object type is implicitly of type any.

I have the following code noImplicitAny:truein tsconfig:

let o = {a: 3};

// works fine
o['a'] = 3;

// reports an error 
// Error:(4, 1) TS7017:Index signature of object type implicitly has an 'any' type.
o['b'] = 3;

What does this error mean?

Here is on the TypeScript playground - be sure to click "Options" and install it noImplicitAny(it seems you don’t remember the options in the available links).

+4
source share
1 answer

The error is caused by the fact that the signature is not explicitly defined.

You can explicitly declare index signaturelike this:

let ox : { [index:string] : number } = {};
ox['b'] = 3; 

I think the reason is o['a'] = 3;not an error due to rule 1 of accessing the notation properties for brackets, which is defined in the following rules from spec :

  • index , ( 3.11.1) , ( ), .
  • , , Any, Number , .
  • , , Any, String Number , .
  • , index Any, String Number , Any.
  • .
+6

Source: https://habr.com/ru/post/1667950/


All Articles