Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@

// TODO remove this eslint-disable
// eslint-disable-next-line import/no-unresolved
import { ActionMatchingPattern, Buffer } from "@redux-saga/types";
import {
ActionMatchingPattern,
Buffer,
SagaIterator,
} from "@redux-saga/types";
import { Action } from "redux";
import {
TakeableChannel,
Expand Down Expand Up @@ -45,6 +49,14 @@ import {
CallEffectDescriptor,
} from "redux-saga/effects";

export type ExtractReturnValue<ReturnType> = ReturnType extends SagaIterator<
infer SagaReturnType
>
? SagaReturnType
: ReturnType extends Promise<infer PromiseReturnType>
? PromiseReturnType
: ReturnType;

export type SagaGenerator<RT, E extends Effect = Effect<any, any>> = Generator<
E,
RT
Expand Down Expand Up @@ -184,22 +196,28 @@ export function putResolve<A extends Action>(
action: A,
): SagaGenerator<A, PutEffect<A>>;

export function call<Args extends any[], Fn extends (...args: Args) => any>(
fn: Fn,
export function call<Args extends unknown[], Return>(
fn: (...args: Args) => Return,
...args: Args
): SagaGenerator<SagaReturnType<Fn>, CallEffect<SagaReturnType<Fn>>>;
): SagaGenerator<
ExtractReturnValue<Return>,
CallEffect<ExtractReturnValue<Return>>
>;

export function call<
Args extends any[],
FnName extends keyof Ctx,
Args extends unknown[],
Return,
Fn extends (this: Ctx, ...args: Args) => Return,
Ctx extends {
[P in Name]: (this: Ctx, ...args: Args) => any;
[Key in FnName]: Fn;
},
Name extends string,
>(
ctxAndFnName: [Ctx, Name],
ctxAndFnName: [Ctx, FnName],
...args: Args
): SagaGenerator<
SagaReturnType<Ctx[Name]>,
CallEffect<SagaReturnType<Ctx[Name]>>
ExtractReturnValue<Return>,
CallEffect<ExtractReturnValue<Return>>
>;
export function call<
Args extends any[],
Expand Down
8 changes: 8 additions & 0 deletions types/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,12 @@ function* mySaga(): Effects.SagaGenerator<void> {
yield* Effects.spawn([obj, "outer"], emitter, handler);
yield* Effects.spawn({ context: obj, fn: obj.outer }, emitter, handler);
yield* Effects.spawn({ context: obj, fn: "outer" }, emitter, handler);

const identity = <T>(x: T): T => x;
// $ExpectType "foo"
yield* Effects.call(identity, "foo" as const);

const ctx = { identity } as const;
// $ExpectType "foo"
yield* Effects.call([ctx, "identity"], "foo" as const);
}