Skip to content

Commit 731a974

Browse files
Basarat Ali SyedBasarat Ali Syed
Basarat Ali Syed
authored and
Basarat Ali Syed
committed
documented discriminated unions + redux with typescript
closes #166
1 parent 9e9d349 commit 731a974

File tree

1 file changed

+67
-3
lines changed

1 file changed

+67
-3
lines changed

docs/types/discriminated-unions.md

+67-3
Original file line numberDiff line numberDiff line change
@@ -107,19 +107,83 @@ function area(s: Shape) {
107107

108108
[references-discriminated-union]:https://github.com/Microsoft/TypeScript/pull/9163
109109

110-
### strictNullChecks
110+
### strictNullChecks
111111

112-
If using strictNullChecks and doing exhaustive checks you should return the `_exhaustiveCheck` variable (of type `never`) as well, otherwise TypeScirpt infers a possible return of `undefined`. So:
112+
If using strictNullChecks and doing exhaustive checks you should return the `_exhaustiveCheck` variable (of type `never`) as well, otherwise TypeScirpt infers a possible return of `undefined`. So:
113113

114114
```ts
115115
function area(s: Shape) {
116116
switch (s.kind) {
117117
case "square": return s.size * s.size;
118118
case "rectangle": return s.width * s.height;
119119
case "circle": return Math.PI * s.radius * s.radius;
120-
default:
120+
default:
121121
const _exhaustiveCheck: never = s;
122122
return _exhaustiveCheck;
123123
}
124124
}
125125
```
126+
127+
### Redux
128+
129+
A popular library that makes use of this is redux.
130+
131+
Here is the [*gist of redux*](https://github.com/reactjs/redux#the-gist) with TypeScript type annotations added:
132+
133+
```ts
134+
import { createStore } from 'redux'
135+
136+
type Action
137+
= {
138+
type: 'INCREMENT'
139+
}
140+
| {
141+
type: 'DECREMENT'
142+
}
143+
144+
/**
145+
* This is a reducer, a pure function with (state, action) => state signature.
146+
* It describes how an action transforms the state into the next state.
147+
*
148+
* The shape of the state is up to you: it can be a primitive, an array, an object,
149+
* or even an Immutable.js data structure. The only important part is that you should
150+
* not mutate the state object, but return a new object if the state changes.
151+
*
152+
* In this example, we use a `switch` statement and strings, but you can use a helper that
153+
* follows a different convention (such as function maps) if it makes sense for your
154+
* project.
155+
*/
156+
function counter(state = 0, action: Action) {
157+
switch (action.type) {
158+
case 'INCREMENT':
159+
return state + 1
160+
case 'DECREMENT':
161+
return state - 1
162+
default:
163+
return state
164+
}
165+
}
166+
167+
// Create a Redux store holding the state of your app.
168+
// Its API is { subscribe, dispatch, getState }.
169+
let store = createStore(counter)
170+
171+
// You can use subscribe() to update the UI in response to state changes.
172+
// Normally you'd use a view binding library (e.g. React Redux) rather than subscribe() directly.
173+
// However it can also be handy to persist the current state in the localStorage.
174+
175+
store.subscribe(() =>
176+
console.log(store.getState())
177+
)
178+
179+
// The only way to mutate the internal state is to dispatch an action.
180+
// The actions can be serialized, logged or stored and later replayed.
181+
store.dispatch({ type: 'INCREMENT' })
182+
// 1
183+
store.dispatch({ type: 'INCREMENT' })
184+
// 2
185+
store.dispatch({ type: 'DECREMENT' })
186+
// 1
187+
```
188+
189+
Using it with TypeScript gives you safety against typo errors, increased refactor-ability and self documenting code .

0 commit comments

Comments
 (0)