Skip to content

Commit 293b0d1

Browse files
committed
Add isFSA helper to createAction
1 parent d499052 commit 293b0d1

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/createAction.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
IfVoid,
66
IsAny
77
} from './tsHelpers'
8+
import isPlainObject from './isPlainObject'
89

910
/**
1011
* An action with a string type and an associated payload. This is the
@@ -295,6 +296,22 @@ export function createAction(type: string, prepareAction?: Function): any {
295296
return actionCreator
296297
}
297298

299+
export function isFSA<
300+
Payload = undefined,
301+
Type extends string = string,
302+
Meta = undefined
303+
>(action: any): action is PayloadAction<Payload, Type, Meta> {
304+
return (
305+
isPlainObject(action) &&
306+
typeof (action as any).type === 'string' &&
307+
Object.keys(action).every(isValidKey)
308+
)
309+
}
310+
311+
function isValidKey(key: string) {
312+
return ['type', 'payload', 'error', 'meta'].indexOf(key) > -1
313+
}
314+
298315
/**
299316
* Returns the action type of the actions created by the passed
300317
* `createAction()`-generated action creator (arbitrary action creators

0 commit comments

Comments
 (0)