I recently integrated Redux into my SPFx webpart (using TypeScript), and everything seems to be working fine. However, I'm struggling with typing the thunk functions and could use some guidance on how to properly type them.
Here's an example of the action I'm trying to dispatch:
export const setListByMiddelware:any = (listId:string) => (dispatch, getState) => {
dispatch(listIdAdded(listId));
dispatch(spApiCallBegan({
method: GET_LIST_ITEMS,
options: {
listId
},
onSuccess: itemsAdded.type
}));
}
Additionally, here is the SharePoint middleware responsible for making the SP call:
import SP from '../../services/SharePointService';
import * as actions from '../SP_API';
const api = store => next => async action => {
if(action.type !== actions.spApiCallBegan.type) {
next(action);
return;
}
const { method, options, onSuccess, onError, onStart } = action.payload;
if(onStart) {
store.dispatch({ type: onStart });
}
next(action);
try {
const result = await SP[method](options);
store.dispatch(actions.spApiCallSuccess(result));
if(onSuccess) {
store.dispatch({
type: onSuccess,
payload: result
});
}
} catch(ex) {
store.dispatch(actions.spApiCallFailed(ex.message));
if(onError) {
store.dispatch({
type: onError,
payload: ex.message
});
}
}
}
export default api;
While I've managed to make things work by simply typing the thunks as any
, I'd prefer to strongly type them since they play a crucial role in my components.
If you're curious about the tools I'm using:
[email protected]
[email protected]
@reduxjs/[email protected]
Despite my efforts, I have encountered difficulties in typing Redux elements, as evidenced by the struggles described above. Any advice or resources would be greatly appreciated.
For TypeScript specifics, I believe I am currently utilizing TS 3.3 within SPFx version 1.11.0.
Edit
After consulting with markerikson, I have explored the official Redux documentation on typing with TypeScript, which can be found here:
https://redux.js.org/recipes/usage-with-typescript#type-checking-middleware
However, despite following the suggested implementations, I encountered errors such as: Type '(listId: string) => (dispatch: any, getState: any) => void' is not assignable to type 'ThunkAction<void, IListSate, unknown, AnyAction>'. Types of parameters 'listId' and 'dispatch' are incompatible. Type 'ThunkDispatch<IListSate, unknown, AnyAction>' is not assignable to type 'string'. regarding the thunk function and 'api' is referenced directly or indirectly in its own type annotation. related to the middleware.
Below is the configuration of my store:
import { configureStore, getDefaultMiddleware } from '@reduxjs/toolkit';
import listRedurcer, { IListSate} from './lists';
import SP_API from './middleware/SP_API';
const store = configureStore({
reducer: listRedurcer,
middleware: [
...getDefaultMiddleware(),
SP_API
],
devTools : {
name: 'ReduxList'
}
});
// Infer the `RootState` and `AppDispatch` types from the store itself
export type RootState = ReturnType<typeof store.getState>;
// Inferred type: {posts: PostsState, comments: CommentsState, users: UsersState}
export type AppDispatch = typeof store.dispatch;
export default store;
In attempting to address the thunk issue, I also experimented with
ThunkAction<void, RootState, string, AnyAction>
but encountered the same error message.