runSaga(,secondparam,)期望使用哪种类型,以及如何制作?

时间:2019-01-15 17:13:17

标签: reactjs typescript redux-saga

我正在尝试编写处理登录的传奇的测试。这是下面的代码以及我遇到的问题:

const fakeStore = await runSaga(
 {
   getState: () => ({...state}),
   dispatch: (action) => dispatchedActions.push(action)
 },
 apis.user.login, // this where the issue is from
 payload
).done;

Tslint错误消息:

Argument of type '(user: Authentication) => Promise<IUserResponse>' is not assignable to parameter of type 'Saga1<Authentication>'.
  Property 'next' is missing in type 'Promise<IUserResponse>' but required in type 'Iterator<any>'.

这里是apis.user

apis{
 user: {
  login: (user: Authentication):Promise<IUserResponse> =>
   axios.post('/api/login', user)
     .then((res) => res.data)
     .catch((res) => res.data || { message:"" }),
 }
}

我不知道如何设置该Saga1类型。我调查了类型定义,这是它的样子:

export function runSaga<A, S, T1>(
  storeInterface: RunSagaOptions<A, S>,
  saga: Saga1<T1>,
  arg1: T1): Task;

Saga1<T1>

type Saga1<T1> = (arg1: T1) => Iterator<any>;

1 个答案:

答案 0 :(得分:0)

Saga1类型是指generator function。这些函数在调用时会返回一个迭代器,但是您不必担心这方面,因为ReduxSaga会调用该函数并与迭代器进行交互。您可以在我为this CodeSandbox创建的this other question中看到使用runSaga的示例。