Skip to content
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

Closed
wants to merge 1 commit into from
Closed

Infer RootState type from RootReducer #75

wants to merge 1 commit into from

Conversation

shtwzrd
Copy link

@shtwzrd shtwzrd commented May 10, 2018

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.

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.
@shtwzrd
Copy link
Author

shtwzrd commented May 10, 2018

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 utility-types in some places, like root-action.ts. Once you're ready to update to 2.8+, those usages could also be replaced with ReturnType.

Copy link
Owner

@piotrwitek piotrwitek left a 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<>`.
Copy link
Owner

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.

@piotrwitek
Copy link
Owner

piotrwitek commented May 13, 2018

@WarreQ there is a next branch with TS 2.8 where I was trying to figure out better HOC approach for 2.8 changes: https://github.com/piotrwitek/react-redux-typescript-guide/commits/next

Perhaps this PR would be more appropriate to put there if it introduces breaking changes to stable master branch

@henrikra
Copy link

@WarreQ Please fix conflicts or close if this is not needed anymore

@piotrwitek
Copy link
Owner

This can be closed as I have already updated to 2.8 some time ago and improved this patterns.

@piotrwitek piotrwitek closed this Aug 16, 2018
@shtwzrd
Copy link
Author

shtwzrd commented Aug 19, 2018

Sorry, didn't mean to leave this sitting so long.

I can see the StateType in the typesafe-actions library accomplishes this inference now so there's no need for this in the guide. Deleting the branch. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants