Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

External libraries supporting typesafe-actions #229

Open
elegos opened this issue Apr 2, 2020 · 1 comment
Open

External libraries supporting typesafe-actions #229

elegos opened this issue Apr 2, 2020 · 1 comment

Comments

@elegos
Copy link

elegos commented Apr 2, 2020

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
Copy link

mikecann commented Apr 22, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants