Hi,
I'm struggling with an issue regarding the DataService interface. It requires the same type to be used for the return type of the load functions and the parameter type of the create, update, updateAll functions.
@Injectable({
providedIn: 'root'
})
export class FlightService implements DataService<Flight, FlightFilter> {
loadById(id: EntityId): Promise<Flight> { ... } // <- type Flight
load(filter: FlightFilter): Promise<Flight[]> { ... }
create(entity: Flight): Promise<Flight> { ... } // <- type Flight
update(entity: Flight): Promise<Flight> { ... } // <- type Flight
updateAll(entity: Flight[]): Promise<Flight[]> { ... } // <- type Flight
delete(entity: Flight): Promise<void> { ... }
[...]
}
This is fine for simple cases but does not work if you have different type for create and for load.
Imagine having Flight defined like:
export type Flight = {
id: number; // <- required id
from: string;
to: string;
date: string;
delayed: boolean;
};
This way when you want to create a new Flight object then you need to provide an id or force Typescript to accept it without id. E.g.:
this.store.create(flight as Flight);
But this is not type safe.
For create I think the following type should be used:
export type CreateFlight = Omit<Flight, 'id'>
Would it by possible to pass an extra type to DataService to let it know what type it should use for create / update?
Hi,
I'm struggling with an issue regarding the
DataServiceinterface. It requires the same type to be used for the return type of theloadfunctions and the parameter type of thecreate,update,updateAllfunctions.This is fine for simple cases but does not work if you have different type for create and for load.
Imagine having Flight defined like:
This way when you want to create a new
Flightobject then you need to provide an id or force Typescript to accept it without id. E.g.:But this is not type safe.
For create I think the following type should be used:
Would it by possible to pass an extra type to
DataServiceto let it know what type it should use for create / update?