我总是用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?
我有两个界面。第一个是别人的孩子。我做了以下几点:
父接口增加索引签名。
使用适当的类型使用as关键字。
完整代码如下:
子接口:
interface UVAmount {
amount: number;
price: number;
quantity: number;
};
父接口:
interface UVItem {
// This is index signature which compiler is complaining about.
// Here we are mentioning key will string and value will any of the types mentioned.
[key: string]: UVAmount | string | number | object;
name: string;
initial: UVAmount;
rating: number;
others: object;
};
反应组件:
let valueType = 'initial';
function getTotal(item: UVItem) {
// as keyword is the dealbreaker.
// If you don't use it, it will take string type by default and show errors.
let itemValue = item[valueType] as UVAmount;
return itemValue.price * itemValue.quantity;
}
我有两个界面。第一个是别人的孩子。我做了以下几点:
父接口增加索引签名。
使用适当的类型使用as关键字。
完整代码如下:
子接口:
interface UVAmount {
amount: number;
price: number;
quantity: number;
};
父接口:
interface UVItem {
// This is index signature which compiler is complaining about.
// Here we are mentioning key will string and value will any of the types mentioned.
[key: string]: UVAmount | string | number | object;
name: string;
initial: UVAmount;
rating: number;
others: object;
};
反应组件:
let valueType = 'initial';
function getTotal(item: UVItem) {
// as keyword is the dealbreaker.
// If you don't use it, it will take string type by default and show errors.
let itemValue = item[valueType] as UVAmount;
return itemValue.price * itemValue.quantity;
}