Builder/Factory pattern for better type propagation in TypeScript/Flow
See original GitHub issueCurrent handleActions(reducerMap, ?defaultState)
signature doesn’t allow the propagation of payload types from actions into reducers, as it is a dictionary type.
A builder or factory pattern like the following would allow for better type propagation
let increment = createAction<void>('INCREMENT');
let decrement = createAction<number>('DECREMENT');
ReducerBuilder()
.addReducer(increment, (state, action) => {})
.addReducer(decrement, (state, action) => {})
.toReducerMap();
The reason being the actions could be typed as Action<void>
and Action<number>
respectively, and .addReducer
can be typed as
function addReducer<TState, TPayload>(action: Action<TPayload>, handler: (state: TState, action: Action<TPayload>)=>{});
This would automated propagate the type into the reducer.
Issue Analytics
- State:
- Created 7 years ago
- Comments:5
Top Results From Across the Web
Understanding design patterns in TypeScript and Node.js
This tutorial runs through several design patterns you can utilize with TypeScript and Node, including singleton and abstract factory.
Read more >TypeScript 4 Design Patterns and Best Practices - Section 1
TypeScript 4 Design Patterns and Best Practices - Section 1 - Discover effective techniques and design patterns for every programming task.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
One year late to the party, but: I’ve used and appreciated @leonyu’s setup for a while now (thank you!!). Partly because of how nice it plays with TypeScript, but I also prefer that pattern over the regular one in
handleActions
.Not saying it is justified adding it to the package though, I do understand @alexgorbatchev’s hesitation.
If anyone like me stumbles across this PR in search of some TypeScript guidance, I go over my implementation of @leonyu’s idea here (along with a
handleActions
comparison).I was looking at this package, and it looked promising for my use, but it didn’t satisfy my need of propagating types without needing runtime
I already have an implementation of it in my own code:
It is trivial to convert the above to less OOP syntax, which would be a lot less verbose. (I used OOP because it would be more familiar to the others on the team)