覆盖导出类型的接口 属性 类型

Overriding interface property type from exported type

我有一个扩展某些类型的接口类型,但我也想“替换”其中一个字段。

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

原来的类型是这样的:

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

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

我希望 assignedPlaces 实际上是一种不同类型的 NewPlaces[]。

我试过类似的方法:

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

但这似乎不起作用。我想不出可以让我覆盖的组合。

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

如果我没有正确理解你的问题,我认为这就是你想要的类型:

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