UnknownAction type in listenerMiddleware when using matcher #4018
-
As I update to RTK 2 I also rewrite my middleware to use listenerMiddleware.startListening({
matcher: isAnyOf(action1, action2, action3),
effect: (action, listenerApi) => {
const user = selectUserDetails(listenerApi.getState())
const { specialData } = action.meta
analyticsApi.trackUsage(action.type, user, specialData)
},
}) When I do this and use EDIT: I also tried it with the Typescript example given in the documentation, but the action is still of type import { createListenerMiddleware, addListener, isAnyOf } from "@reduxjs/toolkit"
import type { TypedStartListening, TypedAddListener } from "@reduxjs/toolkit"
import { refreshMenu, setSelectedItem } from "./designerSlice"
import type { RootState, AppDispatch } from "./store"
export const listenerMiddleware = createListenerMiddleware()
export type AppStartListening = TypedStartListening<RootState, AppDispatch>
export const startAppListening = listenerMiddleware.startListening as AppStartListening
export const addAppListener = addListener as TypedAddListener<RootState, AppDispatch>
startAppListening({
matcher: isAnyOf(refreshMenu, setSelectedItem),
effect: (action, listenerApi) => {
const { specialData } = action.meta
},
}) So it seems RTK won't help me there and I have to cast the action meta myself. Is this correct? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
we're not really sure why, but for some reason the inference order broke in 2.0. You can force the correct order by defining the matcher as a separate variable first: const isSpecialAction = isAnyOf(refreshMenu, setSelectedItem);
startAppListening({
matcher: isSpecialAction,
effect: (action, listenerApi) => {
const { specialData } = action.meta
},
}) |
Beta Was this translation helpful? Give feedback.
we're not really sure why, but for some reason the inference order broke in 2.0.
You can force the correct order by defining the matcher as a separate variable first: