The issue arises due to the absence of an explicit definition for the index signature.
To address this, you can define an index signature
explicitly as shown below:
let objX : { [index:string] : number } = {};
objX['b'] = 3;
The operation o['a'] = 3;
does not result in an error because it aligns with the first rule of bracket notation property access outlined in the following specification rules:
- If the index is a string or numeric literal and the object contains a property corresponding to that literal's name (converted to its string representation in case of a numeric literal), the property access matches the type of that property.
- In cases where the object includes an apparent numeric index signature and the index is of type Any, the Number primitive type, or an enum type, the property access corresponds to the type of that index signature.
- Similarly, if the object features an apparent string index signature and the index is of type Any, the String or Number primitive type, or an enum type, the property access reflects the type of that index signature.
- If the index is of type Any, the String or Number primitive type, or an enum type, the property access defaults to being of type Any.
- Any other scenarios lead to an invalid property access, triggering a compile-time error.