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

Fix type of next parameter in StoreEnhancer type #3776

Merged
merged 1 commit into from
Feb 12, 2023

Conversation

Methuselah96
Copy link
Member

@Methuselah96 Methuselah96 commented May 13, 2020


name: "Fix type of next parameter in StoreEnhancer type"
about: Fix type of next parameter in StoreEnhancer type

PR Type

Does this PR add a new feature, or fix a bug?

Fix a bug. Resolves #3768.

Why should this PR be included?

Without this PR the type for next in StoreEnhancer is incorrect. It assumes that next will create a store with the current enhancers Ext and StateExt already applied to store. Instead, next returns a store with NextExt and NextStateExt generic parameters since the enhancer should work when composed with any other enhancer. It then expects the StoreEnhancer to return a new store creator that will create a store with Ext and NextExt, and StateExt and NextStateExt.

Note that I had to remove the ExtendState type in order to make this work. This type was introduced in #3524, but I wasn't able to get the types working while using ExtendState. I think the types are more clear without it anyway.

Checklist

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Is there an existing issue for this PR?
  • Have the files been linted and formatted?
  • Have the docs been updated to match the changes in the PR?
  • Have the tests been updated to match the changes in the PR?
  • Have you run the tests locally to confirm they pass?

Bug Fixes

What is the current behavior, and the steps to reproduce the issue?

Steps to reproduce are in #3768. There's also a new test for checking the types on stores created from composed enhancers. This test fails in master.

What is the expected behavior?

The types should allow for a generic NextExt and NextStateExt.

How does this PR fix the problem?

It adds those generics.

@netlify
Copy link

netlify bot commented May 13, 2020

Deploy preview for redux-docs ready!

Built with commit c5863d5

https://deploy-preview-3776--redux-docs.netlify.app

@netlify
Copy link

netlify bot commented May 13, 2020

Deploy preview for redux-docs ready!

Built with commit 92cf2a2

https://deploy-preview-3776--redux-docs.netlify.app

Comment on lines 19 to 24
function replaceReducer<NewState, NewActions extends Action>(
nextReducer: Reducer<NewState, NewActions>
) {
store.replaceReducer(nextReducer)
return newStore
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many of the enhancer tests had incorrect replaceReducer methods. Previously they were actually returning the old store without the enhancer extensions applied instead of returning the new store with the enhancers applied. The fix in the types caught this bug.

Copy link
Member Author

@Methuselah96 Methuselah96 May 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also note that the boilerplate that is now required for any enhancer is significantly larger since replaceReducer has to return the extended store. If #3772 gets merged, this boilerplate would be reduced significantly.

@@ -294,3 +328,63 @@ function finalHelmersonExample() {
// typings:expect-error
newStore.getState().wrongField
}

function composedEnhancers() {
Copy link
Member Author

@Methuselah96 Methuselah96 May 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a new test that would fail without this fix.

@samarmohan
Copy link

Can you fix the conflicts?

@codesandbox-ci
Copy link

codesandbox-ci bot commented May 18, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 5a084a6:

Sandbox Source
Vanilla Configuration
Vanilla Typescript Configuration

@markerikson
Copy link
Contributor

I'll try to circle back and actually look at this in the near future. My main observation atm is that this is gonna go onto master, and we have no active plans to actually release the code in master at this point. It's the 4.x branch that matters for the foreseeable future.

@Methuselah96
Copy link
Member Author

Methuselah96 commented Apr 26, 2022

@markerikson No problem, don't mean to guilt you into looking into this but the old types are objectively wrong and so I'd love to get this merged. I'd be happy to create a separate PR to target 4.x.

@Methuselah96
Copy link
Member Author

@markerikson Also #3772 really needs to get merged at some point as well. 😉

@Methuselah96
Copy link
Member Author

Let me know when you're ready to look at either one and I can resolve merge conflicts.

Comment on lines 248 to 254
export type StoreEnhancer<Ext = {}, StateExt = {}> = <NextExt, NextStateExt>(
next: StoreEnhancerStoreCreator<NextExt, NextStateExt>
) => StoreEnhancerStoreCreator<NextExt & Ext, NextStateExt & StateExt>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the main change. All the other changes in this PR just support this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why it doesn't show the old lines but here it is for context:

export type StoreEnhancer<Ext = {}, StateExt = never> = (
  next: StoreEnhancerStoreCreator<Ext, StateExt>
) => StoreEnhancerStoreCreator<Ext, StateExt>

@markerikson
Copy link
Contributor

I'd like to try to do some general Redux org issue triage and cleanup over the next couple weeks, so yeah, getting this knocked out would be high on the priority list.

@Methuselah96 Methuselah96 force-pushed the next-ext-2 branch 5 times, most recently from 2a304f3 to abcad5b Compare February 12, 2023 19:47
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.

StoreEnhancer has incorrect parameter type
3 participants