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
#is there a way to make this mapped type better?
5 messages · Page 1 of 1 (latest)
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
and i would prefer if i didnt have to specify the
x.static, I would prefer ifx.busNumberswere directly accessible
do you mean you want to intersect the objects that are currently understatic/live/nearbytogether?
assuming yes, you could do this:
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
...```
You can choose specific lines to embed by selecting them before copying the link.