尝试理解在TypeScript类中重载方法的最佳方法,并在编译时遇到扩展错误。
有一个抽象类:
export abstract class Collection {
protected _collection: any;
public hydrate(key: number, item: Object): Collection;
public hydrate(key: any, item: any): Collection {
this._collection.set(key, item);
return this;
}
public size(): number {
return this._collection.size;
}
}
具体课程:
export class Tours extends Collection {
public constructor() {
super();
this._collection = new Map();
}
public hydrate(key: number, item: Tour) {
this._collection.set(key, item);
}
}
编译失败,并且具体类(数字和游览)类型的错误无法分配给父方法的数字和对象。
由于Tour类型是自定义类型,不知道如何正确实现此类方案...
怎么做?
答案 0 :(得分:2)
您还需要保留原始签名:
<form action="/foo/bar" method="POST">
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
在你的情况下,我会建议public hydrate(key: number, item: Object) : Collection
public hydrate(key: number, item: Tour) : Collection
public hydrate(key: number, item: Tour) {
return this._collection.set(key, item);
}
通用:
Collection