After creating a generic RESTClient with some abstract functions, I encountered an issue where not all REST actions are implemented. In these cases, I wanted to throw an error.
The problem arose when trying to override the TypeScript method to both return a specified type and throw an error. Adding return null
allowed the code to compile, but it resulted in an unreachable return statement.
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');
}
}
https://i.sstatic.net/khNWm.gif
Can anyone provide guidance on how to correctly implement this pattern in TypeScript (version 3.7.2
)?