How to write typed Middleware in Redux

One minute read in TypeScript Highlights

Just a small snippet which uses TypeScript to define types in middleware declaration. So implementation of middleware becomes possible even without documentation: you just use types to understand what functions do!

import {Action, Dispatch, Middleware, Store} from "redux";
import {AppState} from "./store/AppState";

export function createMiddleware(): Middleware {
  return function (store: Store<AppState>) {
    return function (next: Dispatch) {
      return function (action: Action): Action {
        return next(action)
      }
    }
  }
}

Where AppState is simple interface which represents object stored in redux store.

Redux comes with types, so no need to install any additional typings.


← Ubuntu Yaru Theme for Firefox Hashdeep →