首页 > 解决方案 > '@redux-saga/core' 中的 SagaIterator 有什么用

问题描述

SagaIterator函数发生器有什么用?

import { SagaIterator } from '@redux-saga/core'

function* getCountry(action: ReturnType<typeof actions.country.request>) : SagaIterator {
    try {       
        const selectedCountry = (state: models.InitialStateTypes) => state.selectedCountryInitial        
        const data = yield select(selectedCountry)       
        const response: AxiosResponse<models.CountryInitialResponse> = yield call(
            axios.get , 
            'https://covid19.mathdro.id/api/countries/${data}' , 
            { 
                params: action.payload 
            }
        );
        yield put(actions.country.success(response.data))
    } catch (error) {
        yield put(actions.country.failure(error))
    }
}

标签: typescriptreduxreact-reduxredux-saga

解决方案


查看源代码,我们可以看到作者的评论:

/**
 * Annotate return type of generators with `SagaIterator` to get strict
 * type-checking of yielded effects.
 */
export type SagaIterator = IterableIterator<StrictEffect>

快速浏览一下 StrictEffect 的定义:

export type StrictEffect<T = any> = SimpleEffect<T, any> | StrictCombinatorEffect<T>

export interface StrictCombinatorEffect<T> extends CombinatorEffect<T, StrictEffect<T>> {}

export interface SimpleEffect<T, P> {
  '@@redux-saga/IO': true
  combinator: false
  type: T
  payload: P
}

所以它说的是生成器产生了 redux-saga 效果,如 put、call、select 等。这意味着如果你尝试在生成器中产生其他东西,你应该从 Typescript 得到一个编译错误


推荐阅读