#is there a way to make this mapped type better?

5 messages · Page 1 of 1 (latest)

hybrid sail
#
interface BaseBusStop {
  id: string;
  name: string;
  coords: Coordinate;
}

interface StaticDetails {
  busNumbers: string[];
}

interface LiveDetails {
  arrivalTimes: BusArrivalTime[];
}

interface NearbyDetails {
  distance: number;
}

type BusStopTypeDetails = {
  static: StaticDetails;
  live: LiveDetails;
  nearby: NearbyDetails;
};

export type BusStop<T extends keyof BusStopTypeDetails = never> =
  BaseBusStop & {
    [P in T]: BusStopTypeDetails[P];
  };


let x!: BusStop<"live" | "nearby" | "static">;

x.static
#

i dont like that the intellisense on my BusStop type is like:

type BusStop<T extends keyof BusStopTypeDetails = never> = BaseBusStop & { [P in T]: BusStopTypeDetails[P]; }

and i would prefer if i didnt have to specify the x.static, I would prefer if x.busNumbers were directly accessible

tame glade
#

and i would prefer if i didnt have to specify the x.static, I would prefer if x.busNumbers were directly accessible
do you mean you want to intersect the objects that are currently under static/live/nearby together?

#

assuming yes, you could do this:

wheat pythonBOT
#
mkantor#7432

Preview:```ts
type Coordinate = "Coordinate"
type BusArrivalTime = "BusArrivalTime"

// see https://stackoverflow.com/a/50375286/3625
type UnionToIntersection<U> = (
U extends any ? (k: U) => void : never
) extends (k: infer I) => void
? I
: never

interface BaseBusSto
...```