Skip to content

Commit

Permalink
Add isAction type predicate
Browse files Browse the repository at this point in the history
  • Loading branch information
EskiMojo14 committed Nov 20, 2023
1 parent 6804e81 commit 8472642
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import combineReducers from './combineReducers'
import bindActionCreators from './bindActionCreators'
import applyMiddleware from './applyMiddleware'
import compose from './compose'
import isAction from './utils/isAction'
import __DO_NOT_USE__ActionTypes from './utils/actionTypes'

// types
Expand Down Expand Up @@ -42,5 +43,6 @@ export {
bindActionCreators,
applyMiddleware,
compose,
isAction,
__DO_NOT_USE__ActionTypes
}
10 changes: 10 additions & 0 deletions src/utils/isAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Action } from '../types/actions'
import isPlainObject from './isPlainObject'

export default function isAction(action: unknown): action is Action<string> {
return (
isPlainObject(action) &&
'type' in action &&
typeof (action as Record<'type', unknown>).type === 'string'
)
}
2 changes: 1 addition & 1 deletion src/utils/isPlainObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @param obj The object to inspect.
* @returns True if the argument appears to be a plain object.
*/
export default function isPlainObject(obj: any): boolean {
export default function isPlainObject(obj: any): obj is object {
if (typeof obj !== 'object' || obj === null) return false

let proto = obj
Expand Down

0 comments on commit 8472642

Please sign in to comment.