You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README_SOURCE.md
+76-62
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ _"This guide is a **living compendium** documenting the most important patterns
7
7
8
8
:tada:_Now updated to be compatible with **TypeScript v3.1.6**_:tada:
9
9
10
-
:computer:_Reference implementation of Todo-App with `typesafe-actions`: https://codesandbox.io/s/github/piotrwitek/typesafe-actions-todo-app_:computer:
10
+
:computer:_Reference implementation of Todo-App with `typesafe-actions`: https://codesandbox.io/s/github/piotrwitek/typesafe-actions/tree/master/codesandbox_:computer:
11
11
12
12
### Goals
13
13
- Complete type safety (with [`--strict`](https://www.typescriptlang.org/docs/handbook/compiler-options.html) flag) without losing type information downstream through all the layers of our application (e.g. no type assertions or hacking with `any` type)
@@ -59,9 +59,10 @@ Issues can be funded by anyone and the money will be transparently distributed t
59
59
-[Testing reducer](#testing-reducer)
60
60
-[Async Flow with `redux-observable`](#async-flow-with-redux-observable)
61
61
-[Typing Epics](#typing-epics)
62
-
-[Testing Epics](#testing-epics) 🌟 __NEW__
63
-
-[Selectors](#selectors)
64
-
-[Typing connect](#typing-connect)
62
+
-[Testing Epics](#testing-epics)
63
+
-[Async Flow with `redux-thunk`](#async-flow-with-redux-thunk) 🌟 __NEW__
64
+
-[Selectors with `reselect`](#selectors-with-reselect)
65
+
-[Connect with `react-redux`](#connect-with-react-redux) 🌟 __NEW__
@@ -266,38 +267,24 @@ Adds error handling using componentDidCatch to any component
266
267
267
268
## Redux Connected Components
268
269
269
-
### Caveat with `bindActionCreators`
270
-
**If you try to use `connect` or `bindActionCreators` explicitly and want to type your component callback props as `() =>void` this will raise compiler errors. It happens because `bindActionCreators` typings will not map the return type of action creators to `void`, due to a current TypeScript limitations.**
271
-
272
-
A decent alternative I can recommend is to use `() =>any` type, it will work just fine in all possible scenarios and should not cause any typing problems whatsoever. All the code examples in the Guide with `connect` are also using this pattern.
273
-
274
-
> If there is any progress or fix in regard to the above caveat I'll update the guide and make an announcement on my twitter/medium (There are a few existing proposals already).
275
-
276
-
> There is alternative way to retain type soundness but it requires an explicit wrapping with `dispatch` and will be very tedious for the long run. See example below:
@@ -386,9 +373,9 @@ When creating a store instance we don't need to provide any additional types. It
386
373
## Action Creators
387
374
388
375
> We'll be using a battle-tested library [](https://www.npmjs.com/package/typesafe-actions)
389
-
that automates and simplify maintenace of **type annotations in Redux Architectures** [`typesafe-actions`](https://github.com/piotrwitek/typesafe-actions#typesafe-actions)
376
+
that'll help retain complete type soundness and simplify maintenace of **types in Redux Architectures** [`typesafe-actions`](https://github.com/piotrwitek/typesafe-actions#typesafe-actions)
390
377
391
-
### For more examples and in-depth tutorial you should check [The Mighty Tutorial](https://github.com/piotrwitek/typesafe-actions#behold-the-mighty-tutorial)!
378
+
> You can find more real-world examples and in-depth tutorial in: [Typesafe-Actions - The Mighty Tutorial](https://github.com/piotrwitek/typesafe-actions#behold-the-mighty-tutorial)!
392
379
393
380
A solution below is using a simple factory function to automate the creation of type-safe action creators. The goal is to decrease maintenance effort and reduce code repetition of type annotations for actions and creators. The result is completely typesafe action-creators and their actions.
394
381
@@ -425,12 +412,28 @@ state.todos.push('Learn about tagged union types') // TS Error: Property 'push'
425
412
constnewTodos=state.todos.concat('Learn about tagged union types') // OK
426
413
```
427
414
428
-
#### Caveat: Readonly is not recursive
415
+
#### Caveat - `Readonly` is not recursive
429
416
This means that the `readonly` modifier doesn't propagate immutability down the nested structure of objects. You'll need to mark each property on each level explicitly.
430
417
431
-
To fix this we can use [`DeepReadonly`](https://github.com/piotrwitek/utility-types#deepreadonlyt) type (available in `utility-types` npm library - collection of reusable types extending the collection of **standard-lib** in TypeScript.
418
+
> **TIP:** use `Readonly` or `ReadonlyArray` [Mapped types](https://www.typescriptlang.org/docs/handbook/advanced-types.html)
### For more examples and in-depth tutorial you should check [The Mighty Tutorial](https://github.com/piotrwitek/typesafe-actions#behold-the-mighty-tutorial)!
*__NOTE__: Below you'll find only a short explanation of concepts behind typing `connect`. For more real-world examples please check [Redux Connected Components](#redux-connected-components) section.*
// `state` parameter needs a type annotation to type-check the correct shape of a state object but also it'll be used by "type inference" to infer the type of returned props
// NOTE: We don't need to pass generic type arguments to neither connect nor mapping functions because type inference will do all this work automatically. So there's really no reason to increase the noise ratio in your codebase!
538
-
exportconstFCCounterConnectedVerbose=
520
+
// shorter alternative is to use an object instead of mapDispatchToProps function
521
+
constdispatchToProps= {
522
+
onIncrement: countersActions.increment,
523
+
};
524
+
525
+
// Notice ee don't need to pass any generic type parameters to neither connect nor map functions above
526
+
// because type inference will infer types from arguments annotations automatically
*__NOTE__ (for `redux-thunk`): When using thunk action creators you need to use `bindActionCreators`. Only this way you can get corrected dispatch props type signature like below.*
533
+
534
+
*__WARNING__: As of now (Apr 2019) `bindActionCreators` signature of the latest `redux-thunk` release will not work as below, you need to use updated type definitions that you can find in `/playground/typings/redux-thunk` folder and then add paths overload in your tsconfig like this: `"paths":{"redux-thunk":["typings/redux-thunk"]}`.*
0 commit comments