typescript - '@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))
}
}
解决方案
查看源代码,我们可以看到作者的评论:
/**
* 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 得到一个编译错误
推荐阅读
- flutter - 在flutter中单击gridview后如何显示详细信息
- php - 如何将 HTML 界面(提交 SQL 语句)连接到 PHP 文件,然后将 PHP 连接到我的数据库?
- java - JAVA:无法在循环中返回响应
- c++ - 减号和加号对c ++中的字符串有什么作用
- python-3.x - 我在 pyhton 3.8 中安装 scipy 时出错
- python - 对这个“实验室”感到困惑
- python - 在 altair 中下载选定的数据
- angular - Highcharts 自定义符号
- google-apps-script - 将基于特定列值的范围复制到新工作簿中的多个选项卡中
- javascript - Vue Leaflet 地图图块顺序错误