我正在尝试创建自己的 Typescript 字典,例如类,但在我的代码中使用了一些自定义方法。
我可以像这样创建基本字典:
export interface IDictionary<T> {
[property: string]: T;
};
export class Dictionary<T> implements IDictionary<T> {
[property: string]: T;
constructor() {}
}
这很好用......但是......如果我尝试在这个类中创建一个方法,例如
export class Dictionary<T> implements IDictionary<T> {
[property: string]: T;
constructor() {}
public getValues(): T[] { // this will give the error
return Object.values(this);
}
}
我收到此错误:
Property 'getValues' of type '() => T[]' is not assignable to string index type 'T'
.
这甚至可能吗?如果是,我如何为我的班级创建方法?