【发布时间】:2022-09-23 10:47:00
【问题描述】:
我正在尝试使用枚举值作为对象的键,希望在获取值时保留类型,但我得到Element implicitly has an \'any\' type because expression of type \'string\' can\'t be used to index type。
export enum TaskType {
Classification = \'classification\',
Extraction = \'extraction\'
}
const comparisons: { [name in TaskType]: Function } = {
\'classification\': () => false,
\'extraction\': () => false
}
for (const taskType in comparisons) {
// I expect func to be of type Function, but I get a TypeScript error:
// Element implicitly has an \'any\' type because expression of type \'string\' can\'t be used to index type
const func = comparisons[taskType]
}
我尝试过使用const func = comparisons[taskType as keyof TaskType],但这也不起作用。
标签: typescript