我总是用--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];
需要注意的重要一点是,该想法是key变量来自应用程序中的其他位置,并且可以是对象中的任何键。
我尝试通过以下方式显式转换类型:
let secondValue: string = <string>someObject[key];
还是我的方案无法实现--noImplicitAny
?