'@ redux-saga / core'中SagaIterator的用途是什么

问题描述 投票:0回答:1

[函数发生器中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))
    }
}
typescript redux react-redux redux-saga
1个回答
0
投票

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

/**
 * 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效果,例如放置,调用,选择等。这意味着,如果尝试在生成器中生成其他内容,则应该从Typescript中得到编译错误

© www.soinside.com 2019 - 2024. All rights reserved.