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

Babel Macro to transform `createAction` at compile-time #231

Open avin-kavish opened 4 years ago

avin-kavish commented 4 years ago

Typesafe Actions is an avoidable increase in bundle size. I'm not sure if it's possible but a Babel Macro would allow us to use the library without adding to the bundle size by creating the actions at compile time instead of run-time. It would also increase run-time performance as createAction does not need to be evaluated on each run. A sample transformation would be,

export const foo = createAction('bar/FOO')<string>()

export const foo = payload => ({ type: 'bar/FOO', payload })
export const fooAsync = createAsyncAction(
  'bar/FOO_REQUEST',
  'bar/FOO_SUCCESS',
  'bar/FOO_FAILURE',
)()

export const fooAsync = {
  request: payload => ({ type: 'bar/FOO', payload }),
  sucess: payload => ({ type: 'bar/FOO_SUCCESS', payload }),
  failue: payload => ({ type: 'bar/FOO_FAILURE', payload }) 
}