'How to pass a generic to a function inside callEffect
I have a function that performs api calls and receives a generic that ultimately would be the returned data type:
export const apiCall = async <T>(route: string, method: 'get' | 'post', body: {}): Promise<AxiosResponse<T> | AxiosError<T>> => {
try {
return await axios[method](route, body)
} catch (err) {
return err as AxiosError<T>
}
}
then I have the following saga:
function* signIn (...some args) {
try {
const res = yield* call(apiCall, `/signin`, 'post', { ...body request data });
yield* put(signInSuccess(res.data));
} catch (err) {
yield* put(signInFailure(err as AxiosError<AxiosResponse<SignInFailureError>>));
}
}
this works but I would like to type the return of apiCall function, as you can see my apiCall function requires a generic to define the returned type I get from the axios request whether it is data or an error response.
I tried this (UserInfo is the return type I expect):
const res = yield* call(apiCall<UserInfo>, `/signin`, 'post', { ...body request data });
but it gives me an error:
No overload matches this call.
The last overload gave the following error.
Argument of type 'boolean' is not assignable to parameter of type '{context: unknown; fn: (this: unknown, ...args: any[]) => any; }'
so, how one type a function inside call effect?
PD: just in case, I'm using yield* instead of yield because I'm using the typed-redux-saga library.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
