diff --git a/types/index.test.ts b/types/index.test.ts index cf464629..3d4de12b 100644 --- a/types/index.test.ts +++ b/types/index.test.ts @@ -3,7 +3,7 @@ import * as Effects from "typed-redux-saga"; function* mySaga(): Effects.SagaGenerator { yield* Effects.take("FOO"); // $ExpectType Action - type FooAction = {type: "FOO"}; + type FooAction = { type: "FOO" }; yield* Effects.take("FOO"); // $ExpectType FooAction yield* Effects.takeMaybe("FOO"); // $ExpectType Action yield* Effects.takeMaybe("FOO"); // $ExpectType FooAction @@ -12,14 +12,14 @@ function* mySaga(): Effects.SagaGenerator { 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" @@ -27,7 +27,7 @@ function* mySaga(): Effects.SagaGenerator { yield* Effects.putResolve({ type: "FOO" }); // $ExpectType { type: string; } // $ExpectType number - yield* Effects.call(function*(): SagaIterator { + yield* Effects.call(function* (): SagaIterator { yield* Effects.take(chan); return 22; }); @@ -48,13 +48,13 @@ function* mySaga(): Effects.SagaGenerator { yield* Effects.cps((): number => 22); // $ExpectType FixedTask - let task = yield* Effects.fork(function*(): SagaIterator { + let task = yield* Effects.fork(function* (): SagaIterator { yield* Effects.take(chan); return 22; }); // $ExpectType FixedTask - task = yield* Effects.spawn(function*(): SagaIterator { + task = yield* Effects.spawn(function* (): SagaIterator { yield* Effects.take(chan); return 22; }); @@ -90,30 +90,30 @@ function* mySaga(): Effects.SagaGenerator { yield* Effects.delay(120, true); // $ExpectType never - yield* Effects.throttle(25, "FOO", function*(): SagaIterator { + yield* Effects.throttle(25, "FOO", function* (): SagaIterator { yield* Effects.take(chan); return 22; }); // $ExpectType never - yield* Effects.debounce(25, "FOO", function*(): SagaIterator { + yield* Effects.debounce(25, "FOO", function* (): SagaIterator { yield* Effects.take(chan); return 22; }); // $ExpectType number - yield* Effects.retry(5, 100, function*(): SagaIterator { + yield* Effects.retry(5, 100, function* (): SagaIterator { yield* Effects.take(chan); return 22; }); // $ExpectType number[] yield* Effects.all([ - Effects.call(function*(): Effects.SagaGenerator { + Effects.call(function* (): Effects.SagaGenerator { yield* Effects.take(chan); return 22; }), - Effects.call(function*(): Effects.SagaGenerator { + Effects.call(function* (): Effects.SagaGenerator { yield* Effects.take(chan); return 22; }), @@ -121,11 +121,11 @@ function* mySaga(): Effects.SagaGenerator { // $ExpectType { foo: number; bar: string; } yield* Effects.all({ - foo: Effects.call(function*(): Effects.SagaGenerator { + foo: Effects.call(function* (): Effects.SagaGenerator { yield* Effects.take(chan); return 22; }), - bar: Effects.call(function*(): Effects.SagaGenerator { + bar: Effects.call(function* (): Effects.SagaGenerator { yield* Effects.take(chan); return "hello"; }), @@ -133,11 +133,11 @@ function* mySaga(): Effects.SagaGenerator { // $ExpectType (number | undefined)[] yield* Effects.race([ - Effects.call(function*(): Effects.SagaGenerator { + Effects.call(function* (): Effects.SagaGenerator { yield* Effects.take(chan); return 22; }), - Effects.call(function*(): Effects.SagaGenerator { + Effects.call(function* (): Effects.SagaGenerator { yield* Effects.take(chan); return 22; }), @@ -145,13 +145,54 @@ function* mySaga(): Effects.SagaGenerator { // $ExpectType { foo: number | undefined; bar: string | undefined; } yield* Effects.race({ - foo: Effects.call(function*(): Effects.SagaGenerator { + foo: Effects.call(function* (): Effects.SagaGenerator { yield* Effects.take(chan); return 22; }), - bar: Effects.call(function*(): Effects.SagaGenerator { + bar: Effects.call(function* (): Effects.SagaGenerator { 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 { + subject: T; +} + +function myMessage(): Message { + return { + subject: MessageTypes.MESSAGE, + }; +} + +// Utility type for working out the message subject +type ExtractMessage = M extends Message ? 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> = ResponseTypes[ExtractMessage]; + +export function sendMessage>( + m: M +): Promise> { + // @ts-ignore + return; }