首页 > 解决方案 > 打字稿:在类型上找不到带有“字符串”类型参数的索引签名

问题描述

我正在尝试通过以下方式访问导入对象的属性:

import * as reducers from './reducers';

const reducer = reducers[rule.reducer.name](rule.reducer.args);

在其他地方,我的减速器定义如下:

export const sum = (property: string) => (pageViews: IPageView[]): number =>
  pageViews.reduce((acc, pageView) => Number(acc + pageView[property]), 0);

我也有以下类型定义:

export interface IPageView {
  features: object;
}

interface IConditionQuery {
  property: string;
  value?: any;
}

interface IConditionRule {
  reducer?: {
    name: string;
    args?: any;
  };
  matcher?: {
    name: string;
    args: any;
  };
}

export interface ICondition {
  filter: {
    any: boolean;
    queries: IConditionQuery[];
  };
  rules: IConditionRule[];
}

所以我理解这个问题(我认为)我可能正在访问一个reducer不存在的 a ,因为我传入 a string,我完全不确定我将如何解决这个问题。

我尝试过的是对各种形式的硬编码enums,但没有运气。

编辑:完整的错误信息

semantic error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'typeof import("/Users/dennisyurkevich/dev/edkt/engine/src/reducers")'.
  No index signature with a parameter of type 'string' was found on type 'typeof import("/Users/dennisyurkevich/dev/edkt/engine/src/reducers")'.

标签: typescript

解决方案


推荐阅读