私は常に--noImplicitAnyフラグを使用してTypescriptをコンパイルします。これは、型チェックをできるだけ厳しくしたいので意味があります。
私の問題は、次のコードでエラーが発生すること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];
注意すべき重要な点は、キー変数はアプリケーションの他の場所から取得され、オブジェクト内の任意のキーにすることができるという考え方です。
私は明示的に型をキャストしてみました:
let secondValue: string = <string>someObject[key];
それとも私のシナリオは不可能--noImplicitAny
ですか?