I have a saga defined as the following:
type GenericFunction = (...args: any[]) => any;
interface IFetchSaga<T extends GenericFunction> {
saga: T,
args: Parameters<T>
}
function* triggerChange<T extends GenericFunction>(fetchSaga: IFetchSaga<T>, shouldFetch: boolean) {
// ...do stuff
if(shouldFetch) {
yield call(fetchSaga.saga, ...fetchSaga.args);
}
// ...do stuff
}
When I try to use this saga inside another one, using the call
effect I am not getting the correct typing for the IFetchSaga
args:
function* fetchData(id: number, date: string) {
// ...do fetch
}
function* load() {
// ...do stuff
yield call(triggerChange, {
saga: fetchData,
args: ['10', '2021-03-22'] // I don't get an error saying that '10' should be a number
},
true
);
}
For instance, when I try to execute it directly (as if it was any other function), the typing works correctly:
triggerChange({
saga: fetchData,
args: ['10', '2021-03-22'] // Here I get the error saying that '10' should be a number
});
Is there anything I can do to get the args typing correctly when calling my saga?
Ps.: I know I can define my fetchSaga
param outside of the call effect, but then I would have to always call the IFetchSaga
with a typeof
(and I would like to avoid it):
function* fetchData(id: number, date: string) {
// ...do fetch
}
function* load() {
// ...do stuff
// This would work, but I want to avoid having to do it
const fetchSaga: IFetchSaga<typeof fetchData> = {
saga: fetchData,
args: ['10', '2021-03-22'] // Get the typing error for '10'
};
yield call(triggerChange, fetchSaga, true);
}