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 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:
113
113
114
114
```ts
115
115
function area(s:Shape) {
116
116
switch (s.kind) {
117
117
case"square": returns.size*s.size;
118
118
case"rectangle": returns.width*s.height;
119
119
case"circle": returnMath.PI*s.radius*s.radius;
120
-
default:
120
+
default:
121
121
const _exhaustiveCheck:never=s;
122
122
return_exhaustiveCheck;
123
123
}
124
124
}
125
125
```
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
+
typeAction
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
+
returnstate+1
160
+
case'DECREMENT':
161
+
returnstate-1
162
+
default:
163
+
returnstate
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