来自'@redux-saga/core'的SagaIterator有什么用
What is the use of SagaIterator from '@redux-saga/core'
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))
}
}
查看源码,可以看到作者的评论:
/**
* 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 等。这意味着如果你试图在你的生成器中产生其他东西,你应该得到一个编译错误来自打字稿
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))
}
}
查看源码,可以看到作者的评论:
/**
* 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 等。这意味着如果你试图在你的生成器中产生其他东西,你应该得到一个编译错误来自打字稿