Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a type for take patterns #695

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
5 changes: 5 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ export type SagaGenerator<RT, E extends Effect = Effect<any, any>> = Generator<
RT
>;

type ExtractAction<T> = T extends ActionPattern<infer A> ? A : never;

export function take<A extends Action>(
pattern?: ActionPattern<A>,
): SagaGenerator<A, TakeEffect>;
Expand All @@ -58,6 +60,9 @@ export function take<T>(
multicastPattern?: Pattern<T>,
): SagaGenerator<T, ChannelTakeEffect<T>>;
export function take(pattern?: ActionPattern): SagaGenerator<any, TakeEffect>;
export function take<T extends ActionPattern[]>(
patterns: T,
): SagaGenerator<ExtractAction<T>, TakeEffect>;

export function takeMaybe<A extends Action>(
pattern?: ActionPattern<A>,
Expand Down
4 changes: 4 additions & 0 deletions types/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ function* mySaga(): Effects.SagaGenerator<void> {
yield* Effects.take("FOO"); // $ExpectType Action<any>
type FooAction = { readonly type: "FOO" };
yield* Effects.take<FooAction>("FOO"); // $ExpectType FooAction
const fooActionCreator = (payload: number) => ({ type: 'foo' as const, payload });
const barActionCreator = (payload: string[]) => ({ type: 'bar' as const, payload });
// $ExpectType { type: "foo"; payload: number; } | { type: "bar"; payload: string[]; }
yield* Effects.take([fooActionCreator, barActionCreator]);
yield* Effects.takeMaybe("FOO"); // $ExpectType Action<any>
yield* Effects.takeMaybe<FooAction>("FOO"); // $ExpectType FooAction

Expand Down