I have the following redux-thunk action creator:
function updateInitiator(form_request_id, { to_recipient }) {
return (dispatch) => {
const url = `/some/url`;
const data = { to_recipient };
return fetch(url, { method: 'PUT', body: JSON.stringify(data) }).then(() => {
dispatch(fetchResponses());
});
};
}
Then I declare the type of the function:
type UpdateInitiator = typeof updateInitiator;
I'm trying to derive the type of a bound thunk action. In short, when the action creator is "bound" in react-redux, it automatically calls the function returned with dispatch and then returns the return result of that internal function. I'm trying to declare a type for this behavior. It works if I do it without generics:
type BoundUpdateInitiator = (...args: Parameters<UpdateInitiator>) => ReturnType<ReturnType<UpdateInitiator>>;
But when I try to declare a generic type for any bound function, I'm having some trouble:
type BoundThunk<T> = (...args: Parameters<T>) => ReturnType<ReturnType<T>>;
type BoundUpdateInitiator = BoundThunk<UpdateInitiator>;
This gives me the error:
error TS2344: Type 'T' does not satisfy the constraint '(...args: any) => any'.
236 type BoundThunk<T> = (...args: Parameters<T>) => ReturnType<ReturnType<T>>;
~
error TS2344: Type 'ReturnType<T>' does not satisfy the constraint '(...args: any) => any'.
Type 'unknown' is not assignable to type '(...args: any) => any'.
Type '{}' provides no match for the signature '(...args: any): any'.
236 type BoundThunk<T> = (...args: Parameters<T>) => ReturnType<ReturnType<T>>;
~~~~~~~~~~~~~
error TS2344: Type 'T' does not satisfy the constraint '(...args: any) => any'.
236 type BoundThunk<T> = (...args: Parameters<T>) => ReturnType<ReturnType<T>>;
~
I can vaguely understand that T might be things other than a function, and similarly ReturnType<T> might also not be a function, and this generic type maybe doesn't account for those cases. I'm having trouble understanding how I can account for them, however. Ideally by not allowing them. Any suggestions?