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
For this reason, **we strongly recommend you to only use string action types**.
139
+
140
+
## actionCreator.match
141
+
142
+
Every generated actionCreator has a `.match(action)` method that can be used to determine if the passed action is of the same type as an action that would be created by the action creator.
143
+
144
+
This has different uses:
145
+
146
+
### As a TypeScript TypeGuard
147
+
148
+
This `match` method is a [TypeScript type guard](https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards) and can be used to discriminate the `payload` type of an action.
149
+
150
+
This behavior can be particularly useful when used in custom middlewares, where manual casts might be neccessary otherwise.
151
+
152
+
```typescript
153
+
constincrement= createAction<number>('INCREMENT')
154
+
155
+
functionsomeFunction(action:Action) {
156
+
// accessing action.payload would result in an error here
157
+
if (increment.match(action)) {
158
+
// action.payload can be used as `number` here
159
+
}
160
+
}
161
+
```
162
+
163
+
### With redux-observable
164
+
165
+
The `match` method can also be used as a filter method, which makes it powerful when used with redux-observable:
166
+
167
+
```typescript
168
+
constincrement= createAction<number>('INCREMENT')
169
+
170
+
exportconstepic= (actions$:Observable<Action>) =>
171
+
actions$.filter(increment.match).map(action=> {
172
+
// action.payload can be safely used as number here (and will also be correctly inferred by TypeScript)
0 commit comments