Ich kompiliere Typescript immer mit dem Flag --noImplicitAny. Dies ist sinnvoll, da meine Typprüfung so streng wie möglich sein soll.
Mein Problem ist, dass ich mit dem folgenden Code den Fehler bekomme 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];
Es ist wichtig zu beachten, dass die Schlüsselvariable von einer anderen Stelle in der Anwendung stammt und jeder der Schlüssel im Objekt sein kann.
Ich habe versucht, den Typ explizit umzuwandeln durch:
let secondValue: string = <string>someObject[key];
Oder ist mein Szenario mit einfach nicht möglich --noImplicitAny
?