Home > Enterprise >  Overriding interface property type from exported type
Overriding interface property type from exported type

Time:03-16

I have an interface type that extends some types, but I also want to "replace" one of the fields that comes in.

export interface Place
  extends Collections.Places.Entity<Timestamp, GeoPoint> {
  bounds: Bounds;
  center: Position;
  id: string;
}

The original type looks like this:

export interface Entity<TimestampType = Timestamp, GeoPointType = GeoPoint>
  extends Base<GeoPointType> {

  assignedPlaces?: AllThePlaces[];
  country: Country;
}
 

I want assignedPlaces to actually be a different type of NewPlaces[].

I tried something like:

export interface Place
  extends Pick<Collections.Places.Entity<Timestamp, GeoPoint>, Exclude<keyof Collections.Places.Entity<Timestamp, GeoPoint>, keyof Collections.Places.Entity['assignedPlaces']>> {

but this doesn't seem to be working. I can't figure out a combination that will let me overwrite.

Type 'AllThePlaces[] | undefined' is not assignable to type '(NewPlaces & { toTravel: string; })[] | undefined'.

CodePudding user response:

If I understood your question correctly, I think this is the type you want:

You don't need to supply generic type parameters when you are choosing the same ones as the defaults.

TS Playground

interface Place extends Omit<Collections.Places.Entity, 'assignedPlaces'> {
  assignedPlaces?: NewPlaces[];
  bounds: Bounds;
  center: Position;
  id: string;
}

declare const place: Place;
place.assignedPlaces; // NewPlaces[] | undefined

  • Related