I'm currently using Redux-saga and typescript. But I don't know how to use call
effect with generic function type
in Typescript.
For eg. I have a function with generic type like this:
function identity<T>(arg: T): T {
return arg;
}
And, in saga, I expect the code like this:
...
const result = yield call(identity<string>, "myString")
...
However the compiler shows errors. Of course, I can write
const result = yield call(identity, "myString")
But it's not my expectation. How can I know is there any way/syntax to satisfies my above expectation?
Possible solution is to define type of identity
function and use it to specialize call
invocation.
type identityFunction<T> = (agr: T) => T;
And provide type to call
function
yield call<identityFunction<string>>(identity, 'myString');
Now, if you try to call yield call<identityFunction<string>>
with wrong arguments (also if signature of identity function will not match type identityFunction<T>
), TypeScript will display error.