【问题标题】:No index signature with a parameter of type 'string' was found on type '{}'在“{}”类型上找不到带有“字符串”类型参数的索引签名
【发布时间】:2020-11-03 17:44:11
【问题描述】:

首先我会提出我的问题的布局:

interface FilteredProducts {
    [key: string]: {
        [key: string]: object;
    };
};

我将 filterProducts 初始化为一个空对象,因为我将值放入其中的唯一时间是在 if 中,所以我需要这样做以供以后使用

products: FilteredProducts
let filteredProducts: object = {};

这里我给filterProducts分配一个对象->

if (filters.season && filters.dimension) {
        filteredProducts = (products[filters.season] || {})[filters.dimension];
    };

如果设置了特定过滤器,我想删除此对象的属性

if (productKey && Object.entries(filteredProducts).length > 0) {
       delete filteredProducts[productKey];
}

问题是它说:

元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型“{}”。 在“{}”类型上找不到具有“字符串”类型参数的索引签名

当我尝试删除此键时。

我尝试为 filteredProducts 设置类型 ->

interface FilteredInterface {
    [key: string]: object;
}

然后我明白了

类型“object”不可分配给类型“FilteredInterface”。 类型“{}”中缺少索引签名。

当我尝试将该对象分配给 filterProducts 时。 (第一个if中的代码)

这是这部分的完整代码:

interface FilteredProducts {
    [key: string]: {
        [key: string]: object;
    };
};

const filter = (products: FilteredProducts, filters: UserFiltersInterface): object => {
    let filteredProducts: FilteredInterface = {};
    // console.log('products', products);
    // console.log('filters', filters);
    if (filters.season && filters.dimension) {
        filteredProducts = (products[filters.season] || {})[filters.dimension];
    };
    if (filters.tire_size) {
        // console.log('filteredProducts tire_size', filters.tire_size);
        if (Object.entries(filteredProducts).length > 0) {
            // console.log('filteredProducts', filteredProducts);
            if (filters.tire_size !== 'all') {
                Object.entries(filteredProducts).forEach((product) => {
                    const productKey: string = product[0];
                    const productInfo = product[1];
                    if (productInfo.size !== filters.tire_size) {
                        if (productKey && Object.entries(filteredProducts).length > 0) {
                            delete filteredProducts[productKey];
                        }
                    }
                });
            };
        };
    };
    // console.log('filteredProducts', filteredProducts);
    return filteredProducts;
};

【问题讨论】:

  • 请将这些不同的独立代码块组合成一个有凝聚力的minimal reproducible example 来演示问题。 (理想情况下,还应在TypeScript playground 中包含指向问题的可运行副本的链接。但请确保代码也问题中。)
  • let filteredProducts: { [key: string]: object } = { };
  • @AlekseyL。当我将其设为 interface FilteredInterface { [key: string]: object; } 类型时,我尝试过它不起作用
  • 我试图做 filterProducts[productKey] = undefined 而不是 delete 但它告诉我我不能将 undefined 分配给 readOnly 属性但是 filterProducts 不是 const ?
  • 你肯定在其他地方有问题...delete 选项更好。尽可能不要使用object

标签: reactjs typescript


【解决方案1】:

您需要指定要返回的(products[filters.season])[filters.dimension]FilteredInterface 对象。因为你想把它存储在filteredProducts: FilteredInterface

你可以在这里(替换object):

interface FilteredProducts {
    [key: string]: {
        [key: string]: FilteredInterface;
    };
};

或者在这里(演员表,你将object 保留在FilteredProducts 中:

filteredProducts = (products[filters.season])[filters.dimension] as FilteredInterface;

编辑:见TS Playground

【讨论】:

  • 谢谢,这成功了。剩下的唯一问题是它说它不能删除该属性,或者我不能分配给它,因为它是只读的。但对象不是常量。知道为什么吗?
  • 我不知道......看看我编辑的答案中的操场,它似乎工作正常。
  • 它确实有效 :) 谢谢!感谢快速回复
猜你喜欢
  • 2020-01-06
  • 2021-10-16
  • 2019-10-27
  • 2021-11-27
  • 1970-01-01
  • 2021-12-04
  • 1970-01-01
  • 1970-01-01
  • 2019-11-18
相关资源
最近更新 更多