Code Monkey home page Code Monkey logo

redux-typed-action's Introduction

redux-typed-action

Typesafely create actions and reducers for Redux in TypeScript.
Using this library possibly makes modules Ducks-like.

Install

npm install redux-typed-action --save

Usage

Create action creators and a reducer using createAction and createReducer provided by redux-typed-action.

import { createAction, createReducer } from 'redux-typed-action'

export type CounterState = number

export const counterActions = {
  add: createAction<CounterState, number>('ADD', (state, payload) => state + payload),
  increment: createAction<CounterState>('INCREMENT', state => state + 1),
  decrement: createAction<CounterState>('DECREMENT', state => state - 1),
}

export default createReducer(counterActions, 0)

console.log(counterActions.increment()) // { type: 'INCREMENT' }
console.log(counterActions.add(100))    // { type: 'ADD', payload: 100 }
// counterActions.increment(100)        // compile error: payload of increment() requires undefined
// counterActions.add('100')            // compile error: payload of add() requires number

And use them normally with combineReducers, createStore, bindActionCreators of Redux.

import { bindActionCreators, combineReducers, createStore } from 'redux'
import counterReducer, { CounterState, counterActions } from './modules/counter'  // import the above

export interface State {
  counter: CounterState
  // ...some other states
}

export const store = createStore<State>(combineReducers({
  counter: counterReducer,
  // ...some other reducers
}))

const dispatch = store.dispatch.bind(store)

export const actions = {
  counter: bindActionCreators(counterActions, dispatch),
  // ...some other action creators
}

console.log(store.getState())     // { counter: 0 }
actions.counter.increment()
console.log(store.getState())     // { counter: 1 }
actions.counter.add(100)
console.log(store.getState())     // { counter: 101 }

// actions.counter.increment(100) // compile error: payload of increment() requires undefined
// actions.counter.add('100')     // compile error: payload of add() requires number

Why?

Using Redux requires some boilerplate code. In TypeScript, redundant code increases.
The reducer have to identify the type of the action by the type property. Therefore, each action should be typed explicitly and that type will be shared with the action creator and the reducer.

// cf. https://stackoverflow.com/questions/35482241/how-to-type-redux-actions-and-redux-reducers-in-typescript
const INCREMENT = 'INCREMENT'
const DECREMENT = 'DECREMENT'
const ADD = 'ADD'

interface IncrementAction {
  type: typeof INCREMENT
}
interface DecrementAction {
  type: typeof DECREMENT
}
interface AddAction {
  type: typeof ADD
  payload: number
}

type Action = IncrementAction | DecrementAction | AddAction

const increment: () => IncrementAction = () => ({ type: INCREMENT })
const decrement: () => DecrementAction = () => ({ type: DECREMENT })
const add: (payload: number) => AddAction = payload => ({ type: ADD, payload })

const reducer = (state: number, action: Action) => {
  switch (action.type) {
    case INCREMENT:
      return state + 1
    case DECREMENT:
      return state - 1
    case ADD:
      return state + action.payload
    default:
      return state
  }
}

But it is painful to define type for each action.

This library offers you to declare type of payload as an inline type literal and use that payload immediately once.

const someAction = createAction<State, { prop1: Prop1, prop2?: Prop2 }>('SOME_ACTION', (state, { prop1, prop2 }) => ({ ...state, prop1, prop2 }))

The created action creator requires a parameter of the type declared above. The compiler and IDE will help you. I think this way is safe, efficient and easy to maintain.

API

createAction<State, Payload, Metadata>(type: string, handler: (State, Payload, Metadata) => State, metadataFactory: Payload => Metadata) => ActionCreator<State, Payload, Metadata>

Returns a new action creator.

createReducer<State>(actions: Record<string, ActionCreator<State, any, any>>, initialState: State) => Reducer<State>

Returns a new reducer.

redux-typed-action's People

Contributors

luncheon avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.