Issue
Is there any way to use typescript to set the parameter type on worker saga?
I have this interface
interface ITask{
id: number
task: string,
//other things here
}
And i have this listener and worker:
function* addTaskSaga():any {
yield takeEvery("TASK_ADD", workerAddTaskSaga);
}
function* workerAddTaskSaga({data}:any) {
//other things here
}
This code is working well, however I want to strong type the parameter of the worker, I mean: change {data}:any
to data:ITask
However this throws an exception:
The last overload gave the following error.
Argument of type 'string' is not assignable to parameter of type 'TakeableChannel<unknown>'.ts(2769)
effects.d.ts(207, 17): The last overload is declared here.
Any ideas?
Solution
If question is still relevant here is the answer.
You shuold define some interface which extends Action
from redux and then pass it as type argument to takeEvery
. This will properly type worker function.
// This is Action interface which will hold type and payload.
interface TaskAction extends Action, ITask {type: "TASK_ADD" }
// Alternatively you may use
// interface TaskAction extends Action {type: "TASK_ADD", data: ITask }
interface ITask{
id: number
task: string,
//other things here
}
function* addTaskSaga():any {
yield takeEvery<TaskAction>("TASK_ADD", workerAddTaskSaga);
}
function* workerAddTaskSaga(data:TaskAction) {
// data is of type TaskAction
// data.id is number from ITask
// data.task is string from ITask
}
// Or if you've chosen second form of TaskAction
// function* workerAddTaskSaga({ data } : TaskAction) {
// data is of type ITask
// }
Answered By - Fyodor
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.