-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Infer RootState type from RootReducer #75
Conversation
Use ReturnType (introduced in TypeScript 2.8) to infer RootState's type directly from the map used for constructing the RootReducer. This prevents us from having to repeat ourselves, so when we add a new reducer, we only need to add it in one place, so we can't accidentally add it to the RootState type but forget to update the reducer itself or vice-versa. Upgrading the playground project to TS 2.8 caused issues with two of the HOC examples because of their use of the spread operator, which I was only able to resolve by using Object.assign in one case, and using `as any` in another. Hopefully the TypeScript team will fix this issue soon by introducing the `Spread` type.
Just want to add: super cool project here. :) I was able to make a lot of improvements to my projects once I found your repo. Sorry for the changes to two of your HOC examples. You may want to wait on actually pulling this until this PR lands, so you can keep them as they were. I also noticed you're using |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello, thanks for contribution, I really like your idea and would love to merge it, but I'd like to remove HOC changes and move them to another PR with intention to merge it at later date as suggested by yourself.
Can be imported in connected components to provide type-safety to Redux `connect` function | ||
Can be imported in connected components to provide type-safety to Redux `connect` function. | ||
|
||
Because the RootState is the sum of it's reducers' return types (plus any store Enhancers we may choose to append), TypeScript can infer its shape entirely from Lookup Types and `ReturnType<>`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you mind to change it to make it easier to understand for TS beginners, something like this (don't need to go into implementation details, a concept should be enough):
- TypeScript can infer its shape entirely from rootReducer implementation.
@WarreQ there is a Perhaps this PR would be more appropriate to put there if it introduces breaking changes to stable master branch |
@WarreQ Please fix conflicts or close if this is not needed anymore |
This can be closed as I have already updated to 2.8 some time ago and improved this patterns. |
Sorry, didn't mean to leave this sitting so long. I can see the |
Use ReturnType (introduced in TypeScript 2.8) to infer RootState's
type directly from the map used for constructing the RootReducer.
This prevents us from having to repeat ourselves, so when we add a
new reducer, we only need to add it in one place, so we can't
accidentally add it to the RootState type but forget to update the
reducer itself or vice-versa.
Upgrading the playground project to TS 2.8 caused issues with two
of the HOC examples because of their use of the spread operator,
which I was only able to resolve by using Object.assign in one
case, and using
as any
in another. Hopefully the TypeScript teamwill fix this issue soon by introducing the
Spread
type.