Skip to content

Commit 8472642

Browse files
committed
Add isAction type predicate
1 parent 6804e81 commit 8472642

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import combineReducers from './combineReducers'
44
import bindActionCreators from './bindActionCreators'
55
import applyMiddleware from './applyMiddleware'
66
import compose from './compose'
7+
import isAction from './utils/isAction'
78
import __DO_NOT_USE__ActionTypes from './utils/actionTypes'
89

910
// types
@@ -42,5 +43,6 @@ export {
4243
bindActionCreators,
4344
applyMiddleware,
4445
compose,
46+
isAction,
4547
__DO_NOT_USE__ActionTypes
4648
}

src/utils/isAction.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Action } from '../types/actions'
2+
import isPlainObject from './isPlainObject'
3+
4+
export default function isAction(action: unknown): action is Action<string> {
5+
return (
6+
isPlainObject(action) &&
7+
'type' in action &&
8+
typeof (action as Record<'type', unknown>).type === 'string'
9+
)
10+
}

src/utils/isPlainObject.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @param obj The object to inspect.
33
* @returns True if the argument appears to be a plain object.
44
*/
5-
export default function isPlainObject(obj: any): boolean {
5+
export default function isPlainObject(obj: any): obj is object {
66
if (typeof obj !== 'object' || obj === null) return false
77

88
let proto = obj

0 commit comments

Comments
 (0)