Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
name: 🐛 Bug fix or new feature
about: Fixing a problem with Redux
PR Type
Does this PR add a new feature, or fix a bug?
It makes types safer for middleware.
Why should this PR be included?
Currently, the
next
parameter is typed as theD
type parameter passed, andaction
is typed as theAction
extracted from the dispatch type. Neither of these are a safe assumption:next
would be typed to have all of the dispatch extensions, including the ones earlier in the chain that would no longer apply.next
as the default Dispatch implemented by the base redux store, however this would causenext(action)
to error (as we cannot promiseaction
is actually anAction
) - and it wouldn't account for any following middlewares that return anything other than the action they're given when they see a specific action.action
is not necessarily a known action, it can be literally anything - for example a thunk would be a function with no .type property (soAnyAction
would be inaccurate)This PR changes
next
to be(action: unknown) => unknown
(which is accurate, we have no idea whatnext
expects or will return), and changes theaction
parameter to beunknown
(which as above, is accurate)Checklist
action
is typed asany
#4518Bug Fixes
What is the current behavior, and the steps to reproduce the issue?
As stated above, the types used for
next
andaction
are inaccurate at best, and dangerous at worst.What is the expected behavior?
Middleware should be forced to check what the action is, before using it.
How does this PR fix the problem?
Types
next
andaction
in a way that would force the middleware to type check before using them (return next(action)
still works without issue)Note: this is a breaking change for middleware typed to expect a certain action or dispatch for next. For example:
However, I would argue this is a good change - as explained above, this is inherently a lie to the compiler, and is thus unsafe.
Similar to caught errors, any middleware that (unsafely) wishes to opt out and treat
action
asany
can still do so: