-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
TS Bug found while testing the 2.0.0-beta.4 #3862
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
Comments
Huh, that's surprising. We really didn't change anything about the listener middleware, other than going from Could you put together a project that reproduces this? Also, what version of TS are you on? And is there only one version of the |
i'm not sure what changed the inference behaviour, but it's worth noting that moving the matcher definition outside of the startListening call will work: const isNotificationAction = isAnyOf(addSuccess, addNotice, addError)
notificationEffects.startListening({
matcher: isNotificationAction,
effect: async (action, listenerApi) => {
listenerApi.dispatch(addNotification(action.payload));
},
}); though if i'm honest, this particular usage of listener middleware doesn't seem very necessary? if you're dispatching an action in response to an action you should instead just use extraReducers to respond to the original action within your reducer. const notificationSlice = createSlice({
name: 'notification',
initialState,
reducers: {
addNotification: {
reducer: (state, { payload: { notificationType, notification, autoDismissInMs, id } }: PayloadAction<FullNotificationActionPayload>) => {
state[notificationType].push({ notification, autoDismissInMs, id });
},
prepare: (payload: NotificationActionPayload) => ({ payload: { ...payload, id: nanoid() } })
},
}
extraReducers: (builder) => {
builder.addMatcher(
isAnyOf(addSuccess, addNotice, addError),
(state, action) => notificationSlice.caseReducers.addNotification(state, addNotification(action.payload))
)
}
})
export const { addNotification } = notificationSlice.actions; |
First of all, thanks for the quick replies! @markerikson : Here's a minimal project that shows this error: https://github.com/wirdehall/bug-redux-toolkit-beta @EskiMojo14: Interesting that it changes behaviour when we move it out. I tried it and in my case it still does not work since notification types are not set to string but to "error" | "notice" | "success". Hum, yeah that seams like a good way of going about it. Might change that up in my code. Still, I didn't report this specificlly for my use-case as I can easily solve this by just type-casting. |
for future reference, this seems to be another issue fixed by @Andarist's PR here: microsoft/TypeScript#54183 playground using the build from the PR, proving it fixes the issue: https://tsplay.dev/wE6RVN |
When creating a listener middleware that matches an action using
matcher: isAnyOf(...)
it's no longer getting the correct action type. In this instance it's missing that it's a payload action.Here's a code example:
And where I create my listenerMiddleware:
Here it will complain that action.payload does not exist since action is typed as Action instead of PayloadAction.
This is not a big issue in this example since I shoud probably just use
actionCreateor
as a matcher instead, then action get's the correct type.But the same listener middleware has another listner where I match multiple actions like this:
The same problem here.
This worked prior to upgrading to the beta.
The text was updated successfully, but these errors were encountered: