【问题标题】:Element implicitly has an 'any' type because index expression is not of type 'number'元素隐式具有“任何”类型,因为索引表达式不是“数字”类型
【发布时间】:2018-04-25 04:34:30
【问题描述】:

我在下面收到错误。我知道这段代码是什么意思,但我不知道界面应该是什么样子:

元素隐式具有“任意”类型,因为索引表达式不是“数字”类型。

接口:

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];
    }

问候

【问题讨论】:

    标签: javascript typescript vue.js


    【解决方案1】:

    您的this.listOfSubcategories 是一个数组,但index 是一个字符串。这应该是从数组中读取的数字。

    【讨论】:

    • 谢谢。但是现在让 foo = this.subcategories[parseInt(index)] foo.filter((d :any) => true); throws > 无法调用类型缺少调用签名的表达式
    • 您为什么将d 输入为any?不会是CategoriesMap吗?
    • 是的,但 'any' 更安全。完整的错误是:无法调用类型缺少调用签名的表达式。 “类别”类型没有兼容的调用签名。
    猜你喜欢
    • 2023-04-01
    • 2021-10-11
    • 2017-03-14
    • 2021-08-01
    • 2016-07-23
    • 1970-01-01
    • 2023-02-15
    • 2022-06-12
    • 2021-12-20
    相关资源
    最近更新 更多