我目前正在使用 Redux-Saga、Next.js 和 TypeScript 开展一个项目,但在使用 getServerSideProps
时遇到困难。使用 getServerSideProps
周围的导入包装器,我在“任何”第二行不断收到以下类型错误:
如果我去掉 ': any' 输入,错误显示为从 async 到 => 为:
<块引用>'(context: any) 类型的参数 => Promise<{ props: {}; } | undefined>' 不能分配给类型为 'GetServerSidePropsCallback
/pages/MyPage.tsx
export const getServerSideProps: GetServerSideProps<any> =
wrapper.getServerSideProps(async (context): any=> {. // error is shown here on any
const cookies = nookies.get(context);
if (cookies.userId && cookies.token) {
context.store.dispatch(
{ type: userActionTypes.LOAD_MYPROFILE_REQUEST },
cookies.userId,
);
context.store.dispatch(
{ type: userActionTypes.LOAD_CARDS_REQUEST },
cookies.userId,
cookies.token,
);
context.store.dispatch(END);
await context.store.sagaTask.toPromise();
} else {
return {
props: {},
};
}
});
这是我们的商店:
/store/configureStore.ts
export interface SagaStore extends Store {
sagaTask?: Task;
}
const configureStore: MakeStore<RootState> = () => {
const sagaMiddleware = createSagaMiddleware();
const middlewares = [sagaMiddleware];
const enhancer =
process.env.NODE_ENV === 'production'
? compose(applyMiddleware(...middlewares)) //배포용 미들웨어
: composeWithDevTools(applyMiddleware(...middlewares)); // 개발용 미들웨어
const store = createStore(rootReducer, enhancer);
(store as SagaStore).sagaTask = sagaMiddleware.run(rootSaga);
return store;
};
const wrapper = createWrapper<RootState>(configureStore, {
debug: process.env.NODE_ENV === 'development', // true일 때 디버그가 더 자세히 뜹니다.
});
export default wrapper;