I want to develop a reusable network service component that will handle CRUD requests for an "Item."
For instance, if my "CatService" needs to request a list of "cats," it can utilize a "restService" instance for operations like listing, creating, updating, and deleting:
private restService:RestListService<Cat[]> = RestListService();
...
restService.list(urlToGetCats).then(cats => console.log(listdata));
...
restService.update(urlToUpdateACat, updatedCat);
I have created this generic component, but I feel that it lacks in terms of safety. The class declaration appears as follows:
export class RestListService<T extends Array<Identifiable>> {
private dataSubject$: BehaviorSubject<T> = null;
list(url: string): Promise<T> { }
// ISSUE: I cannot define the `savingInstance` and the returned Promise type properly:
update(updateURL: string, savingInstance: Identifiable): Promise<Identifiable> { }
}
Ideally, I would like to introduce a generic type V
to specify the type of items in the array and enhance type safety for the entire class:
export class RestListService<T extends Array<V extends Identifiable>> {
//This way, the Promise is more type-safe:
update(updateURL: string, savingInstance: Identifiable): Promise<V> { }
}
However, currently, this approach seems to be disallowed.
Is there a way to address the type safety concerns in this scenario?
Thank you for your assistance!