I have a code like this
type ScanSheetEvents =
| { type: "updateStationsList"; stationsList: StationCodeList }
| { type: "reAskPermissions" };
And i want to ensure that some type if of given type of throw and get type inference (something like this)
(_, event) => {
throwIfNotEvent(event, "updateStationsList");
return event.stationsList;
I started to write something like that, but I'm not able to make it work… I want auto-completion on event when type is ensured.
function throwIfNotEvent(
event: ScanSheetEvents,
eventType: ScanSheetEvents["type"],
): event is … {
if (event.type !== eventType) throw Error("Unexpected event" + event.type);
}
maybe someone could help me?
Thanks