piotrwitek / typesafe-actions

Typesafe utilities for "action-creators" in Redux / Flux Architecture
https://codesandbox.io/s/github/piotrwitek/typesafe-actions/tree/master/codesandbox
MIT License
2.41k stars 99 forks source link

External libraries supporting typesafe-actions #229

Open elegos opened 4 years ago

elegos commented 4 years ago

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()
mikecann commented 4 years ago

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