redux-keyable
Small typescript redux utility to improve typesafety between action creators and reducers.
Last updated 2 months ago by justinlivi .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install redux-keyable 
SYNC missed versions from official npm registry.

redux-keyable

Gitlab pipeline status Coverage Status David NPM npm Greenkeeper badge npm type definitions

Because type-safe redux should be easier.

Installation

npm i -s redux-keyable

Why redux-keyable?

Problem

Building an action creator in a type-safe manner is straight forward...

interface ActionCreatorParams {
  value: string;
}

const actionCreator = ({ value }: ActionCreatorParams) => ({
  type: ACTION_TYPE,
  value
}));

But later when we go to write our reducers we have a problem since our reducers will accept ALL of our actions.

interface State {
  storedValue: string;
}

const type AllActions = ???;

const reducer = (state: State, action: AllActions) => {
  // this gets messy fast
}

How do we easily type this?

People have solved this various ways:

Most solutions boil down to type-narrowing via a switch statement keying off a flux standard action type key.

switch (action.type) {
  case ADD:
    return [...state, action.payload];
  case TOGGLE:
    return state.map(item =>
      item.id === action.payload
        ? { ...item, completed: !item.completed }
        : item
    );
  default:
    return state;
}

Can we do better?

Solution

We first define a flux standard action type constant as usual.

const ACTION_TYPE = 'ACTION_TYPE';

Then we type our action using the FluxStandardAction helper interface. We'll use our action definition in both our reducer and our action creator.

interface Action extends FluxStandardAction<typeof ACTION_TYPE> {
  value: string;
}

We use our action definition to define the type of our action creator. The first generic argument is the type of arguments to pass to our creator, and the second is our action type. Passing in our types as generic arguments will help ensure that our action creator matches our reducer.

const actionCreator = createActionCreator<{ value: string }, Action>(
  ({ value }) => ({
    type: ACTION_TYPE,
    value
  })
);

When we go to create our reducers, we use the createKeyableReducer helper method and pass our state type and action type as generic parameters. Our action type and reducer are passed as arguments to the method. Our reducer will only ever get called for actions matching our specified type. No more giant switch statement, and we're still fully type safe!

interface State {
  storedValue: string;
}

const keyableReducer = createKeyableReducer<State, Action>(
  ACTION_TYPE,
  (state, { value }) => ({
    ...state,
    storedValue: value
  })
);

Then we finally combine all our keyable reducers acting on this state into one standard redux reducer. This can be used either as our root reducer or passed to utility methods like reduce-reducers.

// provide a default state
const defaultState = {
  storedValue: 'default'
};

const reducer = combineKeyableReducers<State>(defaultState)(
  keyableReducer,
  // we can pass additional keyable reducers here that will accept the same
  // state type
  reducerNotShownInExample
);

Basic Usage

Create our action interface and our action creator.

// changeDetailsAction.ts
import { createActionCreator, FluxStandardAction } from 'redux-keyable';

export interface DetailsUpdates {
  address: string;
  fullname: string;
}

export const CHANGE_DETAILS = 'CHANGE_DETAILS';

export interface ChangeDetailsAction
  extends FluxStandardAction<typeof CHANGE_DETAILS> {
  payload: DetailsUpdates;
}

export const changeDetails = createActionCreator<
  DetailsUpdates,
  ChangeDetailsAction
>(payload => ({
  payload,
  type: CHANGE_DETAILS
}));

Create our keyable reducer and our standard root reducer.

// changeDetailsReducer.ts
import { combineKeyableReducers, createKeyableReducer } from 'redux-keyable';
import { CHANGE_DETAILS, ChangeDetailsAction } from '../changeDetailsAction';
import { initialState, State } from '../stateDefinition';

export const changeDetailsReducer = createKeyableReducer<
  State,
  ChangeDetailsAction
>(CHANGE_DETAILS, (state, { payload }) => ({
  ...state,
  user: {
    ...state.user,
    ...payload
  }
}));

export const updateDetailsStandardRootReducer = combineKeyableReducers<State>(
  initialState
)(changeDetailsReducer);

License

Licensed under MIT

Current Tags

  • 0.2.18                                ...           latest (2 months ago)

19 Versions

  • 0.2.18                                ...           2 months ago
  • 0.2.17                                ...           4 months ago
  • 0.2.16                                ...           4 months ago
  • 0.2.15                                ...           4 months ago
  • 0.2.14                                ...           4 months ago
  • 0.2.13                                ...           4 months ago
  • 0.2.12                                ...           4 months ago
  • 0.2.11                                ...           4 months ago
  • 0.2.10                                ...           4 months ago
  • 0.2.9                                ...           4 months ago
  • 0.2.8                                ...           4 months ago
  • 0.2.7                                ...           4 months ago
  • 0.2.6                                ...           4 months ago
  • 0.2.5                                ...           4 months ago
  • 0.2.4                                ...           4 months ago
  • 0.2.2                                ...           5 months ago
  • 0.2.1                                ...           6 months ago
  • 0.2.0                                ...           6 months ago
  • 0.1.0                                ...           6 months ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 17
Dependencies (2)
Dev Dependencies (18)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |