Return `yield call` 类型

Return type of a `yield call`

我注意到 yield call 效果的结果在用作

时输入为 any
const data = yield call(f);

f 是一个 () => Promise<number> 函数。

我是不是遗漏了什么或者是 redux-saga 打字限制?

检查 this ongoing thread,tldr;这是打字稿限制

同时,您可以改用这个包:typed-redux-saga

之前

import { call, all } from "redux-saga/effects";
...
// Api.fetchUser return User
// but user has type any
const user = yield call(Api.fetchUser, action.payload.userId);

之后

import { call, all } from "typed-redux-saga";
...
// user now has the correct type User
// NOTE: it's yield*, not yield
const user = yield* call(Api.fetchUser, action.payload.userId);

定义此类型

export type PromiseFn = (...args: any) => Promise<any>;
export type GetT<T> = T extends Promise<infer N> ? N : any;
export type GetFnResult<T extends PromiseFn> = GetT<ReturnType<T>>;

然后

const data: GetFnResult<typeof f> = yield call(f);

您可以使用

const data = (yield call(f)) as number