【发布时间】:2021-10-01 22:22:45
【问题描述】:
假设我想在 Typescript 中定义这个方法:
setResult(guId: string,fieldname: string, data:Array<UsedTsoClusterKey>) {
let octdctruns: OctDctRun[] = [...this.octDctRuns];
const index = octdctruns.findIndex((o) => o.guid === guId);
octdctruns[index][fieldname] = data;
this.octDctRuns = octdctruns;
}
UsedTsoClusterKey 和 OctDctRun 如下所示:
export interface UsedTsoClusterKey {
runGUID: string;
tsoClusterKeyID: string;
tsoClusterKeyVersion: string;
validFrom: DateString;
validUntil: DateString;
}
export interface OctDctRun {
guid: string;
moduleType: string;
runTime: DateString;
calcIntervalFrom: DateString;
calcIntervalUntil: DateString;
triggerType: string;
triggerID: string;
usedTSOClusterKeys: UsedTsoClusterKey[];
}
但是我得到了一行错误 octdctruns[index][fieldname] = data:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'OctDctRun'.
No index signature with a parameter of type 'string' was found on type 'OctDctRun'
我不明白这里的问题。请帮忙!
【问题讨论】:
-
你不能使用
string作为OctDctRun的键,太宽泛了。反正只有usedTSOClusterKeys这个字段可以赋值给UsedTsoClusterKey[],那为什么是参数呢?
标签: javascript typescript vue.js vuetify.js