-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Rewrite MiddlewareArray and gDM for better Dispatch inference #2001
Conversation
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 3ff7137:
|
size-limit report 📦
|
✔️ Deploy Preview for redux-starter-kit-docs ready! 🔨 Explore the source changes: 5ad7019 🔍 Inspect the deploy log: https://app.netlify.com/sites/redux-starter-kit-docs/deploys/61ff52f9ad3e41000799fbb3 😎 Browse the preview: https://deploy-preview-2001--redux-starter-kit-docs.netlify.app |
Weird - I force-pushed the branch, and I can see the new commit in https://github.com/reduxjs/redux-toolkit/commits/feature/gdm-array-types , but the PR isn't updating properly. edit Seems like Github Actions may have been having some issues earlier |
e21d329
to
5ad7019
Compare
46eb2b3
to
3b1e697
Compare
`getDefaultMiddleware` defines an initial type that may or may not include `thunk`, depending on options. From there, `EnhancedStore` tries to extract any extensions to `dispatch` from "the type of all middleware as an array", to fully infer the correct type of `store.dispatch`. This was flaky. In particular, the `MiddlewareArray` type ended up with "a union of all middleware" type, and that meant the extensions weren't always getting read correctly. This commit rewrites the inference to be more correct: - `MiddlewareArray` now tracks an exact tuple type for its contents - `.concat/prepend` now update that middleware tuple type - We exactly extract the dispatch extensions from the tuple, and retain the exact order of the middleware as declared - All the extensions are intersected together with `Dispatch` This should now correctly represent cases like the listener middleware returning an `Unsubscribe` callback in response to `dispatch(addListenerAction())` This commit also drops support for passing `null` as the `extraArg` generic for `ThunkAction`, as that is an outdated pattern and we want to discourage it.
3b1e697
to
3ff7137
Compare
I can't seem to find any issue related to this, is this bug fixed? If yes from which version? I am also facing the unsubscribe type as action bug, is there any work-around? |
@monim67 This is a Pull Request that was merged into 1.8.0 one and a half years ago. |
This is an attempt to rewrite the type inference for the
middleware
arg inconfigureStore
, to better capture cases where a custom middleware overrides the return type ofdispatch
.The motivation for this is specifically an issue with the action listener middleware:
Initial investigation shows that part of the issue is with the middleware itself, due to adding fields like
addListener
to the returned function. Additionally, use ofTypedAddListenerAction
seems to help.However, separate to that, I can reproduce the problem with a dummy middleware:
Given that, it seems that something is wonky with the way
configureStore
infers the extensions todispatch
from the middleware types in conjunction withgetDefaultMiddleware
.While the commit is a mess, the overall approach here is:
MiddlewareArray
type so that it tracks a specific tuple of middleware types, rather than "an array that could be any one of these"dispatch
using that intersected typeWhat I have here mostly works, except for the "null extraArg in ThunkAction" case. This is because theThunkMiddlewareFor
type previously returned a union of twoThunkMiddleware
types with different values forExtraArgument
, oneundefined
and onenull
. This was okay when the middleware types were all getting unioned, but now that I've converted them to be a tuple, that doesn't work.I'd prefer to drop thenull
case if possible. The comment says "this is back-compat, since it was shown in the docs", but I don't know where/when the docs showed that. Unfortunately this seems to be something that is actually used in the wild:https://sourcegraph.com/search?q=context:global+lang:typescript+ThunkAction%3C.%2B%2C.%2B%2C.*null.*%3E&patternType=regexpGranted, several of the hits I looked at are still using the corecreateStore
instead of RTK, but that's still some potential breakage I'd like to avoid if possible.Update
Lenz and I have worked through the original implementation issues. Current status:
extraArg: null
pattern is not something we want people to use (unknown
would be correct today), and that there's only a couple of examples using it with RTK in the wild, we're comfortable treating that as a bug that needs to be fixed rather than a breaking change and just dropping support for that caseMiddlewareArray.typetests.ts
file began failing in this PR. It appears to have been somehow related to the way we do a "package + install" step in our CI builds, while that file also tried to do an import of some local types. The workaround was to rewrite it to actually callconfigureStore
for real instead of faking it at the type level.v1.8.0-integration
branch, and will merge these changes into there