首页 > 解决方案 > runSaga(,secondparam,) 期望哪种类型以及如何制作?

问题描述

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

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>;

标签: reactjstypescriptredux-saga

解决方案


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


推荐阅读