Skip to content

Add option for DataService to specify create / update type #284

@Cselt

Description

@Cselt

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions