代码之家  ›  专栏  ›  技术社区  ›  tbuglc

runSaga(,secondparam,)应为哪种类型?如何创建?

  •  0
  • tbuglc  · 技术社区  · 7 年前

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

    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 回复  |  直到 7 年前
        1
  •  1
  •   Ryan Cogswell    7 年前

    这个 Saga1 类型是指 generator function . 这些函数在被调用时返回一个迭代器,但您不必担心这一点,因为ReduxSaga调用该函数并与迭代器交互。您可以看到一个使用 runSaga 在里面 this CodeSandbox this other question