typesafe-actions icon indicating copy to clipboard operation
typesafe-actions copied to clipboard

External libraries supporting typesafe-actions

Open elegos opened this issue 5 years ago • 1 comments
trafficstars

Hello!

I'd like to keep this issue to gather information about "satellite" projects enhancing the experience with typesafe-actions.

I'll start with my library, others will come as people will reply.

P.S. Maybe a wiki page?

typesafe-actions-reducer-builder

github: https://github.com/elegos/typesafe-actions-reducer-builder npmjs: https://www.npmjs.com/package/typesafe-actions-reducer-builder

Goal of the library

To provide a redux' 100% typed reducer builder, producing a reducer which returns an Immutable<State> object using immer.

Example usage

// actions.ts
import { createAction } from 'typesafe-actions'

export const myAction1 = createAction('action_id')<string>()
export const myAction2 = createAction('action_id')<number>()

// reducers.ts
import createReducerBuilder from 'typesafe-actions-reducer-builder'
import { myAction1, myAction2 } from './actions.ts'

interface State {
  var1: string
  var2: number
}

const initialState: State = {
  var1: '',
  var2: 0,
}

// provides state's interface
const reducer = createReducerBuilder(initialState)
  // provides action's interface
  .handle(myAction1)
  // type-hinted Reducer<TState, TAction>
  .reducer((state, action) => {
    // argument override as immer will take care of it with proxies
    state.var1 = action.payload
    return state
  })
  .handle(myAction2).reducer((state, action) => {
    // type hinted state, action
    state.var2 = action.payload
    return state
  })
  .build()

elegos avatar Apr 02 '20 18:04 elegos

Nice work! This is the sort of thing I was looking for :)

mikecann avatar Apr 22 '20 01:04 mikecann