I always compile TypeScript with the flag --noImplicitAny
. This makes sense as I want my type checking to be as tight as possible.
My problem is that with the following code I get the error:
Index signature of object type implicitly has an 'any' type
interface ISomeObject {
firstKey: string;
secondKey: string;
thirdKey: string;
}
let someObject: ISomeObject = {
firstKey: 'firstValue',
secondKey: 'secondValue',
thirdKey: 'thirdValue'
};
let key: string = 'secondKey';
let secondValue: string = someObject[key];
Important to note is that the idea is that the key variable comes from somewhere else in the application and can be any of the keys in the object.
I've tried explicitly casting the type by:
let secondValue: string = <string>someObject[key];
Or is my scenario just not possible with --noImplicitAny
?