Skip to content

Commit e592043

Browse files
Merge branch 'master' into import-decleration
2 parents fa0d274 + a4310a8 commit e592043

11 files changed

+304
-106
lines changed

README.md

Lines changed: 42 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
11
## React & Redux in TypeScript - Static Typing Guide
22

3-
_"This guide is a **living compendium** documenting the most important patterns and recipes on how to use **React** (and its Ecosystem) in a **functional style with TypeScript** and to make your code **completely type-safe** while focusing on a **conciseness of type annotations** so it's a minimal effort to write and to maintain types in the long run."_
3+
_"This guide is a **living compendium** documenting the most important patterns and recipes on how to use **React** (and its Ecosystem) in a **functional style** using **TypeScript**. It will help you make your code **completely type-safe** while focusing on **inferring the types from implementation** so there is less noise coming from excessive type annotations and it's easier to write and maintain correct types in the long run."_
44

55
[![Join the chat at https://gitter.im/react-redux-typescript-guide/Lobby](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/react-redux-typescript-guide/Lobby)
6+
> #### _Found it useful? Want more updates?_ [**Show your support by giving a :star:**](https://github.com/piotrwitek/react-redux-typescript-guide/stargazers)
67
7-
> #### _Found it useful? Want more updates?_ [**Show your support by giving a :star:**](https://github.com/piotrwitek/react-redux-typescript-patterns/stargazers)
8+
:tada: _Now updated to be compatible with **TypeScript v3.1.6**_ :tada:
89

9-
> _[The Mighty Tutorial](https://github.com/piotrwitek/typesafe-actions#behold-the-mighty-tutorial) for completely typesafe Redux Architecture_ :book:
10-
11-
> _Reference implementation of Todo-App with `typesafe-actions`: https://codesandbox.io/s/github/piotrwitek/typesafe-actions-todo-app_ :computer:
12-
13-
> _Now compatible with **TypeScript v2.8.3** (rewritten using conditional types)_ :tada:
10+
:computer: _Reference implementation of Todo-App with `typesafe-actions`: https://codesandbox.io/s/github/piotrwitek/typesafe-actions-todo-app_ :computer:
1411

1512
### Goals
1613
- 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)
1714
- Make type annotations concise by eliminating redundancy in types using advanced TypeScript Language features like **Type Inference** and **Control flow analysis**
1815
- Reduce repetition and complexity of types with TypeScript focused [complementary libraries](#complementary-libraries)
1916

20-
### Complementary Projects
21-
- Typesafe Action Creators for Redux / Flux Architectures [typesafe-actions](https://github.com/piotrwitek/typesafe-actions)
22-
- Utility Types for TypeScript: [utility-types](https://github.com/piotrwitek/utility-types)
23-
- Reference implementation of Todo-App: [typesafe-actions-todo-app](https://github.com/piotrwitek/typesafe-actions-todo-app)
17+
### Complementary Libraries
18+
- [utility-types](https://github.com/piotrwitek/utility-types) - Collection of generic types for TypeScript, complementing built-in mapped types and aliases - think lodash for reusable types.
19+
- [typesafe-actions](https://github.com/piotrwitek/typesafe-actions) - Typesafe utilities for "action-creators" in Redux / Flux Architecture
2420

2521
### Playground Project
2622
[![Build Status](https://semaphoreci.com/api/v1/piotrekwitek/react-redux-typescript-guide/branches/master/shields_badge.svg)](https://semaphoreci.com/piotrekwitek/react-redux-typescript-guide)
@@ -29,46 +25,45 @@ You should check out Playground Project located in the `/playground` folder. It
2925
> Playground was created in such a way that you can simply clone the repository locally and immediately play around on your own. It will help you to learn all the examples from this guide in a real project environment without the need to create some complicated environment setup by yourself.
3026
3127
## Contributing Guide
32-
If you're planning to contribute please make sure to read the contributing guide: [CONTRIBUTING.md](/CONTRIBUTING.md)
28+
We are open for contributions. If you're planning to contribute please make sure to read the contributing guide: [CONTRIBUTING.md](/CONTRIBUTING.md)
3329

3430
## Sponsor
35-
If you like what we're doing here, you can help us by funding the work on specific issues that you choose by using IssueHunt.io!
31+
This is an independent open-source project created by people investing their free time for the benefit of our community.
3632

37-
This gives you the power to prioritize our work and support the project contributors. Moreover it'll guarantee the project will be updated and maintained in the long run.
33+
If you are using it please consider donating as this will guarantee the project will be updated and maintained in the long run.
3834

39-
> Sponsors are listed in the contributors section at the bottom. If you want to be removed please contact me at: [email protected]
35+
Issues can be funded by anyone and the money will be transparently distributed to the contributors handling a particular issue.
4036

41-
[![issuehunt-image](https://github.com/BoostIO/issuehunt-materials/blob/master/issuehunt-[email protected]?raw=true)](https://issuehunt.io/repos/76996763)
37+
[![Let's fund issues in this repository](https://issuehunt.io/static/embed/issuehunt-button-v1.svg)](https://issuehunt.io/repos/76996763)
4238

4339
---
4440

4541
## Table of Contents
46-
- [Type Definitions & Complementary Libraries](#type-definitions--complementary-libraries)
47-
- [React Types Cheatsheet](#react-types-cheatsheet) 🌟 __NEW__
48-
- [Component Typing Patterns](#component-typing-patterns)
42+
- [Introduction](#introduction)
43+
- [React - Type-Definitions Cheatsheet](#react---type-definitions-cheatsheet)
44+
- [React - Typing Patterns](#react---typing-patterns)
4945
- [Function Components - FC](#function-components---fc)
50-
- [Class Components ](#class-components) 📝 __UPDATED__
46+
- [Class Components ](#class-components)
5147
- [Generic Components](#generic-components)
52-
- [Render Props](#render-props) 🌟 __NEW__
53-
- [Higher-Order Components](#higher-order-components) 📝 __UPDATED__
48+
- [Render Props](#render-props)
49+
- [Higher-Order Components](#higher-order-components)
5450
- [Redux Connected Components](#redux-connected-components)
55-
- [Context](#context)
56-
- [Hooks](#hooks)
57-
- [Redux](#redux)
58-
- [Action Creators](#action-creators) 📝 __UPDATED__
59-
- [Reducers](#reducers) 📝 __UPDATED__
51+
- [Context](#context) 🌟 __NEW__
52+
- [Hooks](#hooks) 🌟 __NEW__
53+
- [Redux - Typing Patterns](#redux---typing-patterns)
54+
- [Action Creators](#action-creators)
55+
- [Reducers](#reducers)
6056
- [State with Type-level Immutability](#state-with-type-level-immutability)
6157
- [Typing reducer](#typing-reducer)
6258
- [Testing reducer](#testing-reducer)
63-
- [Store Configuration](#store-configuration) 📝 __UPDATED__
64-
- [Async Flow](#async-flow) 📝 __UPDATED__
59+
- [Store Configuration](#store-configuration)
60+
- [Async Flow](#async-flow)
6561
- [Selectors](#selectors)
66-
- [Typing connect](#typing-connect) 🌟 __NEW__
62+
- [Typing connect](#typing-connect)
6763
- [Tools](#tools)
6864
- [TSLint](#tslint)
6965
- [Jest](#jest)
70-
- [Enzyme](#enzyme)
71-
- [Living Style Guide](#living-style-guide) 🌟 __NEW__
66+
- [Living Style Guide](#living-style-guide)
7267
- [Common Npm Scripts](#common-npm-scripts)
7368
- [Recipes](#recipes)
7469
- [Baseline tsconfig.json](#baseline-tsconfigjson)
@@ -82,9 +77,9 @@ This gives you the power to prioritize our work and support the project contribu
8277

8378
---
8479

85-
# Type Definitions & Complementary Libraries
80+
# Introduction
8681

87-
### Type Definitions for React & Redux
82+
### Type-Definitions for React & Redux
8883
```
8984
npm i -D @types/react @types/react-dom @types/react-redux
9085
```
@@ -94,19 +89,13 @@ npm i -D @types/react @types/react-dom @types/react-redux
9489
"redux" - (types included with npm package)*
9590
"react-redux" - `@types/react-redux`
9691

97-
> *NB: Guide is based on types from Redux v4.x.x (Beta). To make it work with Redux v3.x.x please refer to [this config](https://github.com/piotrwitek/react-redux-typescript-guide/blob/master/playground/tsconfig.json#L5))
98-
99-
### Complementary Libraries
100-
> Utility libraries **with focus on type-safety** providing a light functional abstractions for common use-cases
101-
102-
- ["utility-types"](https://github.com/piotrwitek/utility-types) - Utility Types for TypeScript (think lodash for types, moreover provides migration from Flow's Utility Types)
103-
- ["typesafe-actions"](https://github.com/piotrwitek/typesafe-actions) - Typesafe Action Creators for Redux / Flux Architectures (in TypeScript)
92+
> *NB: Guide is based on types for Redux >= v4.x.x. To make it work with Redux v3.x.x please refer to [this config](https://github.com/piotrwitek/react-redux-typescript-guide/blob/master/playground/tsconfig.json#L5))
10493
10594
[⇧ back to top](#table-of-contents)
10695

10796
---
10897

109-
# React Types Cheatsheet
98+
# React - Type-Definitions Cheatsheet
11099

111100
#### `React.FunctionComponent<P>` or `React.FC<P>`
112101
Type representing a functional component
@@ -121,13 +110,13 @@ class MyComponent extends React.Component<Props, State> { ...
121110
```
122111
123112
#### `React.ComponentProps<typeof Component>`
124-
Gets component Props type. You don't no need to export Props from your component ever!
113+
Gets type of Component Props, so you don't need to export Props from your component ever! (Works for both FC and Class components)
125114
```tsx
126115
type MyComponentProps = React.ComponentProps<typeof MyComponent>;
127116
```
128117
129118
#### `React.ComponentType<P>`
130-
Type representing union type of (FC | Component)
119+
Type representing union type of (React.FC | React.Component)
131120
```tsx
132121
const withState = <P extends WrappedComponentProps>(
133122
WrappedComponent: React.ComponentType<P>,
@@ -174,7 +163,7 @@ const handleChange = (ev: React.MouseEvent<HTMLDivElement>) => { ... }
174163
175164
---
176165
177-
# Component Typing Patterns
166+
# React - Typing Patterns
178167
179168
## Function Components - FC
180169
@@ -199,7 +188,7 @@ export const FCCounter: React.FC<Props> = props => {
199188
return (
200189
<div>
201190
<span>
202-
{label}: {count}{' '}
191+
{label}: {count}
203192
</span>
204193
<button type="button" onClick={handleIncrement}>
205194
{`Increment`}
@@ -270,7 +259,7 @@ export class ClassCounter extends React.Component<Props, State> {
270259
return (
271260
<div>
272261
<span>
273-
{label}: {count}{' '}
262+
{label}: {count}
274263
</span>
275264
<button type="button" onClick={handleIncrement}>
276265
{`Increment`}
@@ -330,7 +319,7 @@ export class ClassCounterWithDefaultProps extends React.Component<
330319
return (
331320
<div>
332321
<span>
333-
{label}: {count}{' '}
322+
{label}: {count}
334323
</span>
335324
<button type="button" onClick={handleIncrement}>
336325
{`Increment`}
@@ -733,12 +722,13 @@ import { countersActions, countersSelectors } from '../features/counters';
733722
import { FCCounter } from '../components';
734723

735724
type OwnProps = {
736-
initialCount: number;
725+
initialCount?: number;
737726
};
738727

739728
const mapStateToProps = (state: Types.RootState, ownProps: OwnProps) => ({
740729
count:
741-
countersSelectors.getReduxCounter(state.counters) + ownProps.initialCount,
730+
countersSelectors.getReduxCounter(state.counters) +
731+
(ownProps.initialCount || 0),
742732
});
743733

744734
export const FCCounterConnectedExtended = connect(
@@ -970,7 +960,7 @@ export default function ThemeToggleButton(props: Props) {
970960
971961
---
972962
973-
# Redux
963+
# Redux - Typing Patterns
974964
975965
## Action Creators
976966
@@ -1570,8 +1560,7 @@ Object.values = () => [];
15701560
"removeComments": true,
15711561
"sourceMap": true
15721562
},
1573-
"include": ["src", "typings"],
1574-
"exclude": ["src/**/*.spec.*"]
1563+
"include": ["src", "typings"]
15751564
}
15761565

15771566
```

0 commit comments

Comments
 (0)