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

Store enhancers that extend the state and preloaded state cannot be typed cleanly #4283

Closed
antoinetissier opened this issue Feb 11, 2022 · 2 comments

Comments

@antoinetissier
Copy link

antoinetissier commented Feb 11, 2022

Prior Issues

#1648 #3482

What is the current behavior?

#2773 claims to have fixed #1648 and to allow to properly type a storeEnhancer that extends the state.

But it seems to not be sufficient when the enhancer adds some extra preloaded state as well.
Actually, there is a sign of that issue in the dedicated test, which uses any to type the preloaded state.

Another related problem is that the Reducer type does not seem to make sense for an "extra" reducer added through a storeEnhancer.
Indeed, the state the "extra" reducer receives will never be undefined as it will be the result of the initialization after the original reducer is applied.
So state initialization for such an "extra" reducer can only be handled through preloaded state, and not though defining a default state input in the reducer function definition.

// Initialization
// `preloadedState` is possibly undefined.
const nextState = originalReducer(preloadedState, initAction);
// At this stage, `nextState` is not `undefined`.
const extendedState = extraReducer(nextState, initAction);

Steps to Reproduce

This example showcases two TypeScript errors that occur when trying to extend the state and the preloaded state.
The code is inspired from the current test.

What is the expected behavior?

I would expect to be able to avoid the two TypeScript errors in the sandbox above, without resorting to casting or using any.
In particular for the first one, I would expect PreloadedState<S1> & PreloadedState<S2> to be assignable to PreloadedState<S1 & S2>.
I would also expect to be able to use Reducer to type an "extra" reducer added through a store enhancer.

Context

The context is to modularize the Redux store of a module-federated application.

We build a host application that uses Redux for state management and want to allow clients to define extensions to this application.
We were thinking of using storeEnhancer to be able to consume and extend the host application state from an extension.

We are not ruling out other ideas like reducer injection, but it seems less generic than using the storeEnhancer higher-order function, and in particular this would mean that extensions do not have control over the preloaded state and the middlewares.
We also don't need the extensions to inject their reducers at different points in time: we activate all extensions at the same time when the application starts.

Environment Details

Redux 4.1.2

@markerikson
Copy link
Contributor

FWIW, at first glance this is very low priority and not something I would expect us to work on any time soon.

If anyone has potential solutions, please file a PR and we can look at it.

@markerikson
Copy link
Contributor

I think this is improved as of https://github.com/reduxjs/redux/releases/tag/v5.0.0-alpha.5 , and definitely over on the RTK side as of https://github.com/reduxjs/redux-toolkit/releases/tag/v2.0.0-alpha.5 (and you should be using RTK in general).

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

No branches or pull requests

2 participants