我正在使用一个绝对类型的库,我正在尝试创建一个模拟框架,但是我遇到了一个与自身冲突的接口问题:
interface Dictionary<T>{
[name:string]:T;
}
type ItemCollection<T> = Dictionary<T> & {
/**
* Applies an operation to all items in this collection.
* @param delegate An iterative delegate function
*/
forEach(delegate: IterativeDelegate<T>): void;
/**
* Gets the item using a delegate matching function
* @param delegate A matching delegate function
* @returns A T[] whose members have been validated by delegate.
*/
get(delegate: MatchingDelegate<T>): T[];
/**
* Gets the item given by the index.
* @param itemNumber The item number to get.
* @returns The T in the itemNumber-th place.
*/
get(itemNumber: number): T;
getLength(): number;
};
我正在尝试定义一个实现此类型/接口的类:
export class ItemCollectionMock<T> implements Xrm.Collection.ItemCollection<T> {
[index: number]: T;
[key: string]: T;
public itemCollection: T[];
constructor(itemCollection?: T[]) {
this.itemCollection = itemCollection || [];
}
public forEach(delegate: () => void): void {
this.itemCollection.map(delegate);
}
public get(delegate: Xrm.Collection.MatchingDelegate<T>): T[] {
// ... logic here
}
public getLength(): number {
return this.itemCollection.length;
}
public push(item: T): void {
this.itemCollection.push(item);
}
}
但是由于我实现ItemCollection类型中定义的函数所需的函数与IDictionary接口不兼容,因此我得到了编译错误。
我可以通过将[key: string]: T
定义为[key: string]: T | any
来获取要编译的代码,但这似乎不对...
我应该如何创建一个实现此类型/接口的类?
Here is a link到TS Playground版本的代码,显示错误和强大的