'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
종료된 적이 있습니까?언제까지나 답이나 그런 것을 기다릴 수도 있습니다.
두 번째로, 타자 문제입니다.정상적인Dispatch
type 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
'source' 카테고리의 다른 글
배경이 투명한 matplotlib에서 플롯을 내보내는 방법은 무엇입니까? (0) | 2023.07.03 |
---|---|
특정 단추에 기본 '입력'을 설정하는 방법 (0) | 2023.07.03 |
"어레이/포인터 동등성"을 뜻하는 현대 용어는 무엇입니까? (0) | 2023.07.03 |
"git reset"과 "git checkout"의 차이점은 무엇입니까? (0) | 2023.07.03 |
Mongodb Mongo 가져오기가 너무 큽니다.오류 구문 분석 실패 (0) | 2023.07.03 |