Skip to content
Immutable and type-checked state and actions for Redux
Branch: master
Clone or download
Latest commit 725d4a6 Aug 24, 2016
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib
test add tests Jul 7, 2016
.eslintrc initial commit Sep 3, 2015
.gitignore v0.1.3 Sep 18, 2015
.travis.yml fix travis Jul 7, 2016
CHANGELOG.md
LICENSE Initial commit Sep 3, 2015
README.md add tests Jul 7, 2016
index.js refactoring Jun 7, 2016
karma.conf.js v0.1.3 Sep 18, 2015
package.json bump version Aug 24, 2016

README.md

build status

Immutable and type-checked state and actions for Redux (built on tcomb library)

Example

import { createStore, applyMiddleware } from 'redux'
import t from 'tcomb'
import { createCheckedMiddleware, createCheckedReducer, createActionType } from 'redux-tcomb'
import createLogger from 'redux-logger'

// types
const State = t.Integer
const PositiveInteger = t.refinement(t.Integer, n => n >= 0, 'PositiveInteger')
const Action = createActionType({
  INCREMENT: t.interface({ delta: PositiveInteger }),
  DECREMENT: t.interface({ delta: PositiveInteger })
})

// reducer
function reducer(state = 0, action) {
  switch(action.type) {
    case 'INCREMENT' :
      return state + action.delta
    case 'DECREMENT' :
      return state - action.delta
  }
  return state
}

// configure
const store = createStore(
  createCheckedReducer(reducer, State),
  applyMiddleware(
    createCheckedMiddleware(Action),
    createLogger()
  )
)

// ok
store.dispatch({ type: 'INCREMENT', delta: 2 })
store.dispatch(Action.INCREMENT({ delta: 2 }))

// bad payload
store.dispatch({ type: 'INCREMENT', delta: -2 }) // throws [tcomb] Invalid value -2 supplied to Action(INCREMENT)/delta: PositiveInteger

// typo
store.dispatch({ type: 'INCRE', delta: 1 }) // throws [tcomb] Invalid value { "type": "INCRE", "delta": 1 } supplied to Action

API

type Reducer = (state: State, action: Action) => State;
  • createCheckedMiddleware(Action: Type) -> Function
  • createCheckedReducer(reducer: Reducer, State: Type) -> Reducer
  • createActionType(actions: {[key: string]: Type}) -> Type

License

The MIT License (MIT)

You can’t perform that action at this time.