javascript - 元素隐式具有 'any' 类型,因为索引表达式的类型不是 'number'
我遇到下面的错误。我知道这段代码的意思,但我不知道接口应该是什么样子的:
元素隐式具有 'any' 类型,因为索引表达式不是 'number' 类型。
接口 :
export declare interface Category {
id: string;
name: string;
parent_id: string;
}
export declare interface CategoriesMap {
[key: string]: Category;
}
组件 :
private listOfSubcategories: CategoriesMap[] = [];
private selectCategory($event: any, index: string) {
this.category = this.listOfSubcategories[index][$event.target.value];
}
此致
答案#1
你的 this.listOfSubcategories 是一个数组,但 index 是一个字符串。这里应该是一个数字来从数组中读取。