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 call type test #92

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
83 changes: 62 additions & 21 deletions types/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as Effects from "typed-redux-saga";

function* mySaga(): Effects.SagaGenerator<void> {
yield* Effects.take("FOO"); // $ExpectType Action<any>
type FooAction = {type: "FOO"};
type FooAction = { type: "FOO" };
yield* Effects.take<FooAction>("FOO"); // $ExpectType FooAction
yield* Effects.takeMaybe("FOO"); // $ExpectType Action<any>
yield* Effects.takeMaybe<FooAction>("FOO"); // $ExpectType FooAction
Expand All @@ -12,22 +12,22 @@ function* mySaga(): Effects.SagaGenerator<void> {
yield* Effects.take(chan); // $ExpectType "FOO"
yield* Effects.takeMaybe(chan); // $ExpectType "FOO"

yield* Effects.takeEvery("FOO", function*() {}); // $ExpectType never
yield* Effects.takeEvery(chan, function*() {}); // $ExpectType never
yield* Effects.takeEvery("FOO", function* () {}); // $ExpectType never
yield* Effects.takeEvery(chan, function* () {}); // $ExpectType never

yield* Effects.takeLatest("FOO", function*() {}); // $ExpectType never
yield* Effects.takeLatest(chan, function*() {}); // $ExpectType never
yield* Effects.takeLatest("FOO", function* () {}); // $ExpectType never
yield* Effects.takeLatest(chan, function* () {}); // $ExpectType never

yield* Effects.takeLeading("FOO", function*() {}); // $ExpectType never
yield* Effects.takeLeading(chan, function*() {}); // $ExpectType never
yield* Effects.takeLeading("FOO", function* () {}); // $ExpectType never
yield* Effects.takeLeading(chan, function* () {}); // $ExpectType never

yield* Effects.put({ type: "FOO" }); // $ExpectType { type: string; }
yield* Effects.put(chan, "FOO"); // $ExpectType "FOO"

yield* Effects.putResolve({ type: "FOO" }); // $ExpectType { type: string; }

// $ExpectType number
yield* Effects.call(function*(): SagaIterator<number> {
yield* Effects.call(function* (): SagaIterator<number> {
yield* Effects.take(chan);
return 22;
});
Expand All @@ -48,13 +48,13 @@ function* mySaga(): Effects.SagaGenerator<void> {
yield* Effects.cps((): number => 22);

// $ExpectType FixedTask<number>
let task = yield* Effects.fork(function*(): SagaIterator<number> {
let task = yield* Effects.fork(function* (): SagaIterator<number> {
yield* Effects.take(chan);
return 22;
});

// $ExpectType FixedTask<number>
task = yield* Effects.spawn(function*(): SagaIterator<number> {
task = yield* Effects.spawn(function* (): SagaIterator<number> {
yield* Effects.take(chan);
return 22;
});
Expand Down Expand Up @@ -90,68 +90,109 @@ function* mySaga(): Effects.SagaGenerator<void> {
yield* Effects.delay(120, true);

// $ExpectType never
yield* Effects.throttle(25, "FOO", function*(): SagaIterator<number> {
yield* Effects.throttle(25, "FOO", function* (): SagaIterator<number> {
yield* Effects.take(chan);
return 22;
});

// $ExpectType never
yield* Effects.debounce(25, "FOO", function*(): SagaIterator<number> {
yield* Effects.debounce(25, "FOO", function* (): SagaIterator<number> {
yield* Effects.take(chan);
return 22;
});

// $ExpectType number
yield* Effects.retry(5, 100, function*(): SagaIterator<number> {
yield* Effects.retry(5, 100, function* (): SagaIterator<number> {
yield* Effects.take(chan);
return 22;
});

// $ExpectType number[]
yield* Effects.all([
Effects.call(function*(): Effects.SagaGenerator<number> {
Effects.call(function* (): Effects.SagaGenerator<number> {
yield* Effects.take(chan);
return 22;
}),
Effects.call(function*(): Effects.SagaGenerator<number> {
Effects.call(function* (): Effects.SagaGenerator<number> {
yield* Effects.take(chan);
return 22;
}),
]);

// $ExpectType { foo: number; bar: string; }
yield* Effects.all({
foo: Effects.call(function*(): Effects.SagaGenerator<number> {
foo: Effects.call(function* (): Effects.SagaGenerator<number> {
yield* Effects.take(chan);
return 22;
}),
bar: Effects.call(function*(): Effects.SagaGenerator<string> {
bar: Effects.call(function* (): Effects.SagaGenerator<string> {
yield* Effects.take(chan);
return "hello";
}),
});

// $ExpectType (number | undefined)[]
yield* Effects.race([
Effects.call(function*(): Effects.SagaGenerator<number> {
Effects.call(function* (): Effects.SagaGenerator<number> {
yield* Effects.take(chan);
return 22;
}),
Effects.call(function*(): Effects.SagaGenerator<number> {
Effects.call(function* (): Effects.SagaGenerator<number> {
yield* Effects.take(chan);
return 22;
}),
]);

// $ExpectType { foo: number | undefined; bar: string | undefined; }
yield* Effects.race({
foo: Effects.call(function*(): Effects.SagaGenerator<number> {
foo: Effects.call(function* (): Effects.SagaGenerator<number> {
yield* Effects.take(chan);
return 22;
}),
bar: Effects.call(function*(): Effects.SagaGenerator<string> {
bar: Effects.call(function* (): Effects.SagaGenerator<string> {
yield* Effects.take(chan);
return "hello";
}),
});

// $ExpectType { type: "valid_return" };
yield* Effects.call(sendMessage, myMessage());
}

async function otherText() {
// $ExpectType { type: "valid_return" };
await sendMessage(myMessage());
}

export enum MessageTypes {
MESSAGE = "message",
OTHER_MESSAGE = "other_message",
}

export interface Message<T extends MessageTypes> {
subject: T;
}

function myMessage(): Message<MessageTypes.MESSAGE> {
return {
subject: MessageTypes.MESSAGE,
};
}

// Utility type for working out the message subject
type ExtractMessage<M> = M extends Message<infer T> ? T : never;
type IResponseTypes = { [key in MessageTypes]: unknown };
export interface ResponseTypes extends IResponseTypes {
[MessageTypes.MESSAGE]: { type: "valid_return" };
[MessageTypes.OTHER_MESSAGE]: { type: "other_return" };
}

// Utility type to lookup the Message Response type using the messaage subject from the ResponseTypes type.
export type Response<T extends Message<any>> = ResponseTypes[ExtractMessage<T>];

export function sendMessage<M extends Message<MessageTypes>>(
m: M
): Promise<Response<M>> {
// @ts-ignore
return;
}