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
**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.**
628
-
629
-
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.
630
-
631
-
> 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).
632
-
633
-
> 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:
@@ -1131,12 +1121,28 @@ state.todos.push('Learn about tagged union types') // TS Error: Property 'push'
1131
1121
const newTodos =state.todos.concat('Learn about tagged union types') // OK
1132
1122
```
1133
1123
1134
-
#### Caveat: Readonly is not recursive
1124
+
#### Caveat - `Readonly` is not recursive
1135
1125
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.
1136
1126
1137
-
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.
1127
+
> **TIP:** use `Readonly` or `ReadonlyArray` [Mapped types](https://www.typescriptlang.org/docs/handbook/advanced-types.html)
*__NOTE__: Below you'll find only a short explanation of concepts behind typing `connect`. For more advanced scenarios and common use-cases (`redux-thunk` and more...) 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!
1420
+
// shorter alternative is to use an object instead of mapDispatchToProps function
1421
+
const dispatchToProps = {
1422
+
onIncrement: countersActions.increment,
1423
+
};
1424
+
1425
+
// Notice ee don't need to pass any generic type parameters to neither connect nor map functions above
1426
+
// because type inference will infer types from arguments annotations automatically
Copy file name to clipboardExpand all lines: README_SOURCE.md
+39-46Lines changed: 39 additions & 46 deletions
Original file line number
Diff line number
Diff line change
@@ -266,20 +266,6 @@ Adds error handling using componentDidCatch to any component
266
266
267
267
## Redux Connected Components
268
268
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:
@@ -425,12 +411,28 @@ state.todos.push('Learn about tagged union types') // TS Error: Property 'push'
425
411
constnewTodos=state.todos.concat('Learn about tagged union types') // OK
426
412
```
427
413
428
-
#### Caveat: Readonly is not recursive
414
+
#### Caveat - `Readonly` is not recursive
429
415
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
416
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.
417
+
> **TIP:** use `Readonly` or `ReadonlyArray` [Mapped types](https://www.typescriptlang.org/docs/handbook/advanced-types.html)
*__NOTE__: Below you'll find only a short explanation of concepts behind typing `connect`. For more advanced scenarios and common use-cases (`redux-thunk` and more...) 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!
523
+
// shorter alternative is to use an object instead of mapDispatchToProps function
524
+
constdispatchToProps= {
525
+
onIncrement: countersActions.increment,
526
+
};
527
+
528
+
// Notice ee don't need to pass any generic type parameters to neither connect nor map functions above
529
+
// because type inference will infer types from arguments annotations automatically
0 commit comments