source

'ThunkAction' 유형의 인수를 'AnyAction' 유형의 매개 변수에 할당할 수 없습니다.

nicesource 2023. 7. 3. 22:57
반응형

'ThunkAction' 유형의 인수를 'AnyAction' 유형의 매개 변수에 할당할 수 없습니다.

다음과 같은 유형의 스크립트를 사용하여 redux thunk 함수를 설정했습니다.

export const fetchActivities = (): AppThunk => async dispatch => {
    dispatch(fetchActivitiesRequest())

    try {
        const res = await fetch("/activities")
        const body = await res.json()

        dispatch(fetchActivitiesSuccess(body))
    } catch (err) {
        dispatch(fetchActivitiesError(err))
    }
}

AppThunk는 ThunkAction 유형에서 파생된 유형으로, 다음과 같은 유형 매개 변수가 있습니다.

export type AppThunk<ReturnType = void> = ThunkAction<ReturnType, {}, null, Action<string>>

내 문제는 내 작업에 대한 단위 테스트를 설정하려고 할 때와 다음과 같은 thunk 작업을 발송하려고 할 때입니다.

const middlewares = [thunk]
const mockStore = configureMockStore(middlewares)
const store = mockStore({ activities: [] })

store.dispatch(actions.fetchActivities())

다음 오류 메시지가 표시됩니다.

Argument of type 'ThunkAction<void, {}, null, Action<string>>' is not assignable to parameter of type 'AnyAction'.
  Property 'type' is missing in type 'ThunkAction<void, {}, null, Action<string>>' but required in type 'AnyAction'.ts(2345)

저는 이 문제에 대한 해결책을 찾으려고 노력했지만 성공하지 못했습니다.대부분의 제안은 발송을 위해 제네릭에 임의의 매개변수를 추가하도록 시도하라고 합니다.dispatch<any>(actions.fetchActivities())이렇게 하면 타이핑 오류가 중지되지만, try and catch 문 내의 모든 작업은 무시되고 thunk의 첫 번째 작업만 발송됩니다.

이 문제를 해결하는 방법에 대해 제안할 수 있는 사람이 있습니까?

이것들은 두 가지 별개의 문제입니다.어떤 식으로든 입력은 런타임 동작에 영향을 미치지 않습니다.

우선:그래서 다른 행동을 하지 않는 당신의 생각은 오직 한 가지 의미만을 가질 수 있습니다.다른 전화는dispatch연락이 닿지 않을 가능성이 높습니다에 전화하셨습니까?fetch종료된 적이 있습니까?언제까지나 답이나 그런 것을 기다릴 수도 있습니다.

두 번째로, 타자 문제입니다.정상적인Dispatchtype just는 기본값당 thunk를 지원하지 않습니다.사용자의 솔루션은Dispatch<any>괜찮습니다.그렇지 않으면, 당신은 당신의 것을 캐스팅할 수 있습니다.dispatch에 대한 기능.ThunkDispatch에서redux-thunk꾸러미

편집: 죄송합니다. RTK 오류와 똑같이 생겼지만 뭔가 다릅니다. (오답일 수 있습니다.)

이것은 RTK에서 알려진 버그로 현재 이를 해결할 수 있는 PR이 열려 있습니다. 현재 제가 아파서 마무리를 짓지 못했지만, 새로운 버전을 내놓을 때까지 CI 빌드를 사용할 수 있습니다.다음을 통해 설치할 수 있습니다.

yarn add https://pkg.csb.dev/reduxjs/redux-toolkit/commit/933aa4a1/@reduxjs/toolkit또는npm i https://pkg.csb.dev/reduxjs/redux-toolkit/commit/933aa4a1/@reduxjs/toolkit

미들웨어 = [thunk]를 미들웨어로 변경 = getDefaultMiddleware => getDefaultMiddleware()

언급URL : https://stackoverflow.com/questions/60049490/argument-of-type-thunkaction-is-not-assignable-to-parameter-of-type-anyaction

반응형