debugtypescriptCritical
How do I prevent the error "Index signature of object type implicitly has an 'any' type" when compiling typescript with noImplicitAny flag enabled?
Viewed 0 times
errorindexsignaturehowobjecthastypescriptanywithflag
Problem
I always compile TypeScript with the flag
My problem is that with the following code I get the error:
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:
Or is my scenario just not possible with
--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' typeinterface 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 = someObject[key];Or is my scenario just not possible with
--noImplicitAny?Solution
Adding an index signature will let TypeScript know what the type should be.
In your case that would be
However, this also enforces all of the property types to match the index signature. Since all of the properties are a
While index signatures are a powerful way to describe the array and 'dictionary' pattern, they also enforce that all properties match their return type.
Edit:
If the types don't match, a union type can be used
With union types, it's better if you let TypeScript infer the type instead of defining it.
Although that can get a little messy if you have a lot of different types in the index signatures. The alternative to that is to use
In your case that would be
[key: string]: string;interface ISomeObject {
firstKey: string;
secondKey: string;
thirdKey: string;
[key: string]: string;
}However, this also enforces all of the property types to match the index signature. Since all of the properties are a
string it works.While index signatures are a powerful way to describe the array and 'dictionary' pattern, they also enforce that all properties match their return type.
Edit:
If the types don't match, a union type can be used
[key: string]: string|IOtherObject;With union types, it's better if you let TypeScript infer the type instead of defining it.
// Type of `secondValue` is `string|IOtherObject`
let secondValue = someObject[key];
// Type of `foo` is `string`
let foo = secondValue + '';Although that can get a little messy if you have a lot of different types in the index signatures. The alternative to that is to use
any in the signature. [key: string]: any; Then you would need to cast the types like you did above.Code Snippets
interface ISomeObject {
firstKey: string;
secondKey: string;
thirdKey: string;
[key: string]: string;
}// Type of `secondValue` is `string|IOtherObject`
let secondValue = someObject[key];
// Type of `foo` is `string`
let foo = secondValue + '';Context
Stack Overflow Q#32968332, score: 408
Revisions (0)
No revisions yet.