https://github.com/gcanti/redux-tcomb
Immutable and type-checked state and actions for Redux
https://github.com/gcanti/redux-tcomb
Last synced: 6 months ago
JSON representation
Immutable and type-checked state and actions for Redux
- Host: GitHub
- URL: https://github.com/gcanti/redux-tcomb
- Owner: gcanti
- License: mit
- Archived: true
- Created: 2015-09-03T10:54:45.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2016-08-24T12:23:29.000Z (about 9 years ago)
- Last Synced: 2025-04-13T22:05:24.778Z (7 months ago)
- Language: JavaScript
- Homepage:
- Size: 42 KB
- Stars: 211
- Watchers: 9
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-redux - redux-tcomb - Immutable and type-checked state and actions for Redux. (Code Architecture)
README
[](https://travis-ci.org/gcanti/redux-tcomb)
Immutable and type-checked state and actions for Redux (built on [tcomb](https://github.com/gcanti/tcomb) library)
# Example
```js
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
```js
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)