运行流类型时生成的类型正确吗?

时间:2018-08-02 04:16:19

标签: flowtype redux-saga

我正在使用Flowtype v0.75和redux-saga运行项目 目前,我在尝试确定如何为我的sagas提供正确类型时遇到一些问题,尤其是在我尝试将它们组合时:

ActivateAccountSaga.js

// @flow
import Alert from 'react-s-alert';
import { combineSagas, handleError } from 'util/Saga';
import { call, put, take, all } from 'redux-saga/effects';
import { history } from 'modules/LocationModule';
import {
  activateAccount,
  sendActivationEmail,
  sendActivationEmailPending,
  sendActivationEmailFulfilled,
  sendActivationEmailRejected,
} from 'bundles/Auth/modules/ActivateAccountModule';
import AuthAPI from 'bundles/Auth/apis/AuthAPI';
import config from 'config/index';

export function* activateAccountWorker(api: AuthAPI): Generator<*, *, *> {
  while (true) {
    const { payload } = yield take(activateAccount().type);
    try {
      const response = yield call([api, api.activateAccount], payload);
      yield call(history.push, config.route.identity.signIn);
      yield call(Alert.success, response.description);
    } catch (e) {
      yield call(history.push, config.route.identity.signIn);
      yield call(handleError, e);
    }
  }
}

export function* sendActivationEmailWorker(api: AuthAPI): Generator<*, *, *> {
  while (true) {
    const { payload } = yield take(sendActivationEmail().type);
    try {
      yield put(sendActivationEmailPending());
      const response = yield call([api, api.sendActivationMail], payload);
      yield put(sendActivationEmailFulfilled(response));
      yield call(Alert.success, response.description, { timeout: 30000 });
      yield call(history.push, config.route.identity.signIn);
    } catch (e) {
      yield put(sendActivationEmailRejected(e));
      yield call(handleError, e);
    }
  }
}

export function* activateAccountSaga(api: AuthAPI): Generator<*, *, *> {
  yield all(combineSagas([
    [activateAccountWorker, api],
    [sendActivationEmailWorker, api],
  ]));
}

const api = new AuthAPI();
export default [activateAccountSaga, api];

这是util / Saga.js

// @flow
import get from 'lodash/get';
import Alert from 'react-s-alert';
import { actions } from 'react-redux-form';
import { call, put, spawn, all, select } from 'redux-saga/effects';

export const refineSaga = (saga: * | Array<*>) => {
  // Saga is a generator function without params
  if (typeof saga === 'function') {
    return [saga];
  }

  // Ensures that a saga in the form [saga, params...] was given
  if (Array.isArray(saga) && typeof saga[0] === 'function') {
    return saga;
  }

  throw new TypeError(`Unexpected saga type: ${saga.toString()}`);
};

export const combineSagas = (sagas: Array<Function | *>): any[] => sagas.map(saga => spawn(...refineSaga(saga)));

在运行v0.75流时,出现以下错误:

Error ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈ src/bundles/Auth/sagas/ActivateAccountSaga.js:48:6

generator function [1] requires another argument from function type [2] in type argument Fn.

     src/bundles/Auth/sagas/ActivateAccountSaga.js
 [1]  16│ export function* activateAccountWorker(api: AuthAPI): Generator<*, *, *> {
        :
      45│
      46│ export function* activateAccountSaga(api: AuthAPI): Generator<*, *, *> {
      47│   yield all(combineSagas([
      48│     [activateAccountWorker, api],
      49│     [sendActivationEmailWorker, api],
      50│   ]));
      51│ }

     flow-typed/npm/redux-saga_v0.16.x.js
 [2] 863│     <R, Fn: () => R>(fn: Fn): SpawnEffect<null, Fn, []>,

Error ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈ src/bundles/Auth/sagas/ActivateAccountSaga.js:48:29

A callable signature is missing in AuthAPI [1] but exists in function type [2] in type argument Fn.

     src/bundles/Auth/sagas/ActivateAccountSaga.js
      45│
 [1]  46│ export function* activateAccountSaga(api: AuthAPI): Generator<*, *, *> {
      47│   yield all(combineSagas([
      48│     [activateAccountWorker, api],
      49│     [sendActivationEmailWorker, api],
      50│   ]));
      51│ }

     flow-typed/npm/redux-saga_v0.16.x.js
 [2] 863│     <R, Fn: () => R>(fn: Fn): SpawnEffect<null, Fn, []>,

如您所见,flow告诉我应该提供另一个参数,但是我是以基于数组的方式进行操作的,因此替代方案是什么,因此flow对此感到满意?

1 个答案:

答案 0 :(得分:0)

问题出在这里:

export const refineSaga = (saga: * | Array<*>): Array<*> =>

export const combineSagas = (sagas: any[]): any[] => sagas.map(saga => spawn(...refineSaga(saga)));