Here is part of a function I’ve written to add actions to a Redux store:
const dispatch = useDispatch<AppDispatch>(); // typed action dispatcher
const sliceActions = customSlice.actions // get the action callbacks from the custom store slice
let dispatchActions: any = {} // return object, dispatch functions mapped to custom action names
for (let action in sliceActions) {
const cb = sliceActions[action] // type: void | Redux action creator types
dispatchActions[action] = (input?: any) => { // assign a callback function to each key, this callback invokes the dispatcher on cb
if (input) {
return dispatch(cb({ input }))
}
return dispatch(cb())
}
}
This works when run, but TypeScript shows an error on the cb
function calls which can have the void
type, saying Type 'void' has no call signatures.
If I change the type assertion for cb
to const cb = sliceActions[action] as Function
or as any
, the error goes away.
Why is TypeScript flagging the void
type as having no call signatures in the first place? And will the type assertion as Function
on a callback cause any other problems?