typescript - 打字稿:在类型上找不到带有“字符串”类型参数的索引签名
问题描述
我正在尝试通过以下方式访问导入对象的属性:
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")'.
解决方案
推荐阅读
- python - 将 json 转换为 csv 以获得相似的键
- css - 为什么一个 4mb 的透明 PNG 需要 10-20 秒才能加载为重复的 CSS 背景纹理?
- angular - Angular Universal“找不到模块文件(/src/app/.ts)”
- python - 解析以引号开头的tsv文件
- sql - 比较同一个表的两列中的字符串
- jquery - 通用预输入字段和初始化程序
- compression - 存储/压缩黑白PNG的最优化方式是什么
- swift - 如何使用 Swift 读取 AXRoleDescription 和可访问性层次结构?
- postgresql - PostgreSQL中的&&是什么意思?
- android - 我想在列表中以 recyclerview 的形式获取所有订单的价格