The error "Element implicitly has an 'any' type because expression of type 'number' can't be used to index type" occurs when we try to access a property of an object without a type definition.
Let us consider the following code which is going to raise the same error as stated above:
TypeScript is saying that we have not defined a type for the "romanNumber" object. This could lead to errors such as the wrong usage of an invalid key to access a property in the "romanNumbers" object.
In order to fix this issue, we need to type the "romanNumbers" object. The easiest way to type an object is to create an interface as follows:
Then we need to pass the object type to the object as shown below:
Now, let's run the entire error free code with proper typing :
Now when we try to access the "romanNumber" object with the index, TypeScript knows that the incoming index value is a "number" type and is also the same as the expected key type for accessing this object.
When defining or fetching an object or an array, always define its typing in order to access the values without type errors.