npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

redux-typed-action

v0.0.2

Published

Typesafely create actions and reducers for Redux in TypeScript

Downloads

3

Readme

redux-typed-action

Typesafely create actions and reducers for Redux in TypeScript.
Using this library possibly makes modules Ducks-like.

Install

npm install redux-typed-action --save

Usage

Create action creators and a reducer using createAction and createReducer provided by redux-typed-action.

import { createAction, createReducer } from 'redux-typed-action'

export type CounterState = number

export const counterActions = {
  add: createAction<CounterState, number>('ADD', (state, payload) => state + payload),
  increment: createAction<CounterState>('INCREMENT', state => state + 1),
  decrement: createAction<CounterState>('DECREMENT', state => state - 1),
}

export default createReducer(counterActions, 0)

console.log(counterActions.increment()) // { type: 'INCREMENT' }
console.log(counterActions.add(100))    // { type: 'ADD', payload: 100 }
// counterActions.increment(100)        // compile error: payload of increment() requires undefined
// counterActions.add('100')            // compile error: payload of add() requires number

And use them normally with combineReducers, createStore, bindActionCreators of Redux.

import { bindActionCreators, combineReducers, createStore } from 'redux'
import counterReducer, { CounterState, counterActions } from './modules/counter'  // import the above

export interface State {
  counter: CounterState
  // ...some other states
}

export const store = createStore<State>(combineReducers({
  counter: counterReducer,
  // ...some other reducers
}))

const dispatch = store.dispatch.bind(store)

export const actions = {
  counter: bindActionCreators(counterActions, dispatch),
  // ...some other action creators
}

console.log(store.getState())     // { counter: 0 }
actions.counter.increment()
console.log(store.getState())     // { counter: 1 }
actions.counter.add(100)
console.log(store.getState())     // { counter: 101 }

// actions.counter.increment(100) // compile error: payload of increment() requires undefined
// actions.counter.add('100')     // compile error: payload of add() requires number

Why?

Using Redux requires some boilerplate code. In TypeScript, redundant code increases.
The reducer have to identify the type of the action by the type property. Therefore, each action should be typed explicitly and that type will be shared with the action creator and the reducer.

// cf. https://stackoverflow.com/questions/35482241/how-to-type-redux-actions-and-redux-reducers-in-typescript
const INCREMENT = 'INCREMENT'
const DECREMENT = 'DECREMENT'
const ADD = 'ADD'

interface IncrementAction {
  type: typeof INCREMENT
}
interface DecrementAction {
  type: typeof DECREMENT
}
interface AddAction {
  type: typeof ADD
  payload: number
}

type Action = IncrementAction | DecrementAction | AddAction

const increment: () => IncrementAction = () => ({ type: INCREMENT })
const decrement: () => DecrementAction = () => ({ type: DECREMENT })
const add: (payload: number) => AddAction = payload => ({ type: ADD, payload })

const reducer = (state: number, action: Action) => {
  switch (action.type) {
    case INCREMENT:
      return state + 1
    case DECREMENT:
      return state - 1
    case ADD:
      return state + action.payload
    default:
      return state
  }
}

But it is painful to define type for each action.

This library offers you to declare type of payload as an inline type literal and use that payload immediately once.

const someAction = createAction<State, { prop1: Prop1, prop2?: Prop2 }>('SOME_ACTION', (state, { prop1, prop2 }) => ({ ...state, prop1, prop2 }))

The created action creator requires a parameter of the type declared above. The compiler and IDE will help you. I think this way is safe, efficient and easy to maintain.

API

createAction<State, Payload, Metadata>(type: string, handler: (State, Payload, Metadata) => State, metadataFactory: Payload => Metadata) => ActionCreator<State, Payload, Metadata>

Returns a new action creator.

createReducer<State>(actions: Record<string, ActionCreator<State, any, any>>, initialState: State) => Reducer<State>

Returns a new reducer.