How can I set the types of a function using call()
?
I have this function:
export function apiFetch<T>(url: string): Promise<T> {
return fetch(url).then(response =>
{
if (!response.ok) throw new Error(response.statusText)
return response.json().then(data => data as T);
}
)
}
This function can be used like:
let resp = await apiFetch<ServerResponse>("http://localhost:51317/Task");
By using the function as you can see in the above piece of code, resp
is correctly string-typed. So intellisense offers me all the attributes of the ServerResponse
interface.
However, this function has to be call inside a worker from redux-saga
which does not allow, async functions:
function* refreshTaskSaga():any {
yield takeEvery("TASK_REFRESH", workerRefreshTaskSaga);
}
function* workerRefreshTaskSaga() {
//I need to call the function here
}
I try to call it using yield + call, as redux-saga
documentation said:
a) let resp = yield call(apiFetch, "http://localhost:51317/Task");
b) let resp = yield call(apiFetch<ServerResponse>, "http://localhost:51317/Task");
The first option, execute the function as expected, however resp
has any
type.
The second options throws me an exception.
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; }'.ts(2769)
effects.d.ts(499, 17): The last overload is declared here.
Any idea of the correct syntax to call it and don't lose types?
call
coming from ? – UndertakingCall
comes fromredux-saga
– Marsupium