用于取消订阅 firestore 和 Redux-Thunk 的 Typescript 类型
Typescript types for unsubscribe from firestore and Redux-Thunk
我的 Thunk Action 看起来像:
export const doTestCall = (teamId:string): ThunkAction<()=>void, RootState, null, MoodAction> => {
return dispatch => {
const unsubscribe = db.doc("id").onSnapshot(doc =>{
//whatever
});
return unsubscribe;
}
}
在我的 React 组件中,我有以下钩子:
useEffect(() => {
const unsubscribe = dispatch(doSomething()) as ()=>void;
return () => unsubscribe();
}, []);
这段代码确实有效。
我有一个问题,如何取消强制
as ()=>void
如果我这样做,代码会在“return 行”中抱怨:
const unsubscribe: (dispatch: ThunkDispatch<RootState, null, MoodAction>, getState: () => RootState, extraArgument: null) => void
Expected 3 arguments, but got 0.ts(2554)
index.d.ts(9, 3): An argument for 'dispatch' was not provided.
谢谢,
好像ThunkAction<()=>void, RootState, null, MoodAction>
不符合你的中间件定义
我假设是这样的
ThunkAction<()=>void, RootState, unknown, AnyAction>
//or
ThunkAction<()=>void, RootState, any, AnyAction>
会更好,假设您的 dispatch
类型完全包含 thunk 中间件的类型。
如果您使用的是官方推荐的 redux 工具包,请在此处记录设置正确的 dispatch
类型:https://redux-toolkit.js.org/tutorials/typescript
我的 Thunk Action 看起来像:
export const doTestCall = (teamId:string): ThunkAction<()=>void, RootState, null, MoodAction> => {
return dispatch => {
const unsubscribe = db.doc("id").onSnapshot(doc =>{
//whatever
});
return unsubscribe;
}
}
在我的 React 组件中,我有以下钩子:
useEffect(() => {
const unsubscribe = dispatch(doSomething()) as ()=>void;
return () => unsubscribe();
}, []);
这段代码确实有效。 我有一个问题,如何取消强制
as ()=>void
如果我这样做,代码会在“return 行”中抱怨:
const unsubscribe: (dispatch: ThunkDispatch<RootState, null, MoodAction>, getState: () => RootState, extraArgument: null) => void
Expected 3 arguments, but got 0.ts(2554)
index.d.ts(9, 3): An argument for 'dispatch' was not provided.
谢谢,
好像ThunkAction<()=>void, RootState, null, MoodAction>
不符合你的中间件定义
我假设是这样的
ThunkAction<()=>void, RootState, unknown, AnyAction>
//or
ThunkAction<()=>void, RootState, any, AnyAction>
会更好,假设您的 dispatch
类型完全包含 thunk 中间件的类型。
如果您使用的是官方推荐的 redux 工具包,请在此处记录设置正确的 dispatch
类型:https://redux-toolkit.js.org/tutorials/typescript