【发布时间】:2020-03-26 21:43:07
【问题描述】:
我已经实现了一个抽象的通用 RESTClient。有时并非所有 REST 操作都已实现,因此我想在这种情况下抛出错误。
如果我重写了 TypeScript 期望我返回指定类型的方法,即使我抛出了错误。如果我添加return null,它会编译,但返回是无法访问的。
export class RESTClient<E extends { id: number }, D = Omit<E, 'id'>> extends Client {
protected path: string;
public constructor(path: string, token?: string) {
super(token);
this.path = path;
}
public update(entity: E) {
return this.clientInstance.put<E>(`${this.path}/${entity.id}`, entity);
}
}
export class ItemClient extends RESTClient<Item> {
public constructor(token?: string) {
super('items', token);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public delete(_entity: Item) {
throw new Error('Not supported by API');
}
}
如何在 TypeScript(版本 3.7.2)中正确实现这一点?
【问题讨论】:
标签: typescript overloading abstract-class