Skip to content

Commit

Permalink
Updated README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
teneko committed Oct 29, 2021
1 parent b75607b commit 3173e57
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The library provides:

* Middleware that makes it work.

* Many TypeScript helper types
* TypeScript helper types and functions

These are described in detail below.

Expand Down Expand Up @@ -220,6 +220,33 @@ sagaMiddleware.run(rootSaga)

Additionally, all the helper functions will throw a custom `Error` subclass `ConfigurationError` if `promiseMiddleware` was not properly included in the store.

## TypeScript helper types

`promiseAction.types ` does not really exist- it only exists as TypeScript-type to make use of `typeof`:

```js
const promiseAction = promiseActionFactory<number>().simple("MY_ACTION");
declare const typeOfActionThatGotCreatedFromTheSimpleOrAdvancedActionCreator: typeof promiseAction.types.action;
declare const typeOfPromiseThatGotCreatedOfPromiseMiddleware: typeof promiseAction.types.promise;
const promise = store.dispatch(promiseAction()).meta.promise; // OR
// = store.dispatch(promiseAction()) as any as typeof promiseAction.types.promise;
```

`redux-saga` cannot infer the parameters and return type of `promiseAction` correctly when using the call effect or equivalent, so you can use the pre-typed sagas:

```js
const { implement, resolve, reject } = promiseAction.sagas;
// Instead of this...
call(implementPromiseAction, promiseAction(), () => 2);
// ... use this for better TypeScript support:
call(promiseAction.sagas.implement, promiseAction(), () => 2);
```

## Contributing

### Building & Testing

`package.json` defines the usual scripts:
Expand Down
4 changes: 2 additions & 2 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,15 +273,15 @@ describe("rejectPromiseAction", function () {
expect(store.getState().rejected === error).toBeTruthy();
});

test("should throw ArgumentError", function () {
it("should throw ArgumentError", function () {
const { caughtMiddlewareError, promiseAction, store } = setup(sagas.rejectSaga);
const bogusPromiseAction = () => ({ type: promiseAction.toString() }); // mimics promise action but doesn't have proper meta
store.dispatch(bogusPromiseAction());
store.dispatch(sagas.controlAction({}));
expect(caughtMiddlewareError() instanceof ArgumentError).toBeTruthy();
});

test("should throw ConfigurationError", function () {
it("should throw ConfigurationError", function () {
const { caughtMiddlewareError, promiseAction, store } = setup(sagas.rejectSaga, { withMiddleware: false });
store.dispatch(promiseAction());
store.dispatch(sagas.controlAction({}));
Expand Down

0 comments on commit 3173e57

Please sign in to comment.