Closed Dudeonyx closed 5 years ago
Any updates? How can I help?
Any updates? How can I help?
Ah, I'm actually done, Full type inference!, I got side tracked forking redux-starter-kit, converting it to typescript and incorporating my changes.
For now I'll make a PR with the new typings. let's discuss the other(somewhat breaking) changes and possible benefits another time
I forked this project and made some changes to how the actions are typed
Here it is
I published a temporary alt package to be used for testing as
robodux-alt
1
First of all the action interfaces are simpler because in the all action types are of the form
actionName: (payload: string) => Action<string>
The only thing that changes is the payload type so why not simply specifyactionName: string
and convert it to the first form internally2
Second if an actions interface is provided it is used to typecheck the actions field when creating the slice
3
Third I had to do some weird typing so that if an actions interface is not provided the action creators are typed as
(payload?: any) => Action<any>
Note: all of the above were done without editing the actual code at all, just the types. The code below required heavy editing and is mostly experimental
4
Fourth I refactored the code a bit(meaning a lot) so I could create a createSliceAlt that detects if the initial state is an object and automatically create additional selectors for each key in it
The additional selectors are name in the form:
get${slice}${key}
So if you have an intial state object like above in a slice called
hi
and with keystest
andwow
, the selectors created would be:getHi
,getHiWow
andgetHiTest
And they do exactly what their names suggest.However Trying to correctly get the return types of the selectors is proving to be difficult, likely impossible as Typescript currently is. Right now the type of the any of the selectors in this case for example would be
(state: State) => State | SliceState | string | number
It has a return type including every single type in the state, even those from other slices.
I'm not satisfied with this hence why I created a different function instead but I refactored the original create functions sub-functions to reduce code duplication.