I've found that it's universally faster.
In TypeScript exhaustive checks work, not sure about flow.
The default case is when indexing into the object returns undefined.
I like it for performance reasons and also because it seems more functional to me, containing values (which can also be functions when needed) and not expressions.
Turns out there's some trickery at work, but I never have to think about it because it's in another file in my actual projects.
typeFullEvent<EP,TextendskeyofEP>={type:T}&EP[T]typeFullEvents<EP>={[TinkeyofEP]:FullEvent<EP,T>}typeAnyEvent<EP>=FullEvents<EP>[keyofEP]// Your code begins hereinterfaceEventPayloads{increment:{increment:number}reset:{}}typeEvent=AnyEvent<EventPayloads>typeState=numberexportconstmatchReducer=(state:State,event:Event):State=>{switch(event.type){case"increment":returnstate+event.incrementcase"reset":return0}// Uncommenting this disables exhaustiveness checking// throw new Error("Unsupported Event")}typeReducer<S,EP,TextendskeyofEP>=(state:S,event:FullEvent<EP,T>,)=>SconstreducersLookup:{[TinEvent["type"]]:Reducer<State,EventPayloads,T>}={increment:(s,e)=>s+e.increment,reset:()=>0,}exportconstlookupReducer=(state:State,event:Event):State=>{constreducer=reducersLookup[event.type]asReducer<State,EventPayloads,typeofevent.type>if(!reducer){// this is fine, because our exhaustiveness check already happened when constructing the lookupthrownewError("Unsupported Event")}returnreducer(state,event)}
Also, in my case all variadic properties are under event.data, not root of event, so event always looks like {type: T, data: EventPayloads[T]}, but I wanted to implement your case.
Making them take the correct Action object is the more hacky part.
But I guess it makes sense, switch takes place inside function body, which does more inference, while the lookup is statically declared functions, which always requires more annotation.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I've found that it's universally faster.
In TypeScript exhaustive checks work, not sure about flow.
The default case is when indexing into the object returns
undefined
.I like it for performance reasons and also because it seems more functional to me, containing values (which can also be functions when needed) and not expressions.
Do you have a code snippet?
Based on benchmark or personal impression?
Turns out there's some trickery at work, but I never have to think about it because it's in another file in my actual projects.
Also, in my case all variadic properties are under
event.data
, not root ofevent
, soevent
always looks like{type: T, data: EventPayloads[T]}
, but I wanted to implement your case.It appears it is pretty easy in TS
And with Flow too
Making them take the correct Action object is the more hacky part.
But I guess it makes sense,
switch
takes place inside function body, which does more inference, while the lookup is statically declared functions, which always requires more annotation.