redux: Error in types for middleware (typescript 2.4.1)

It is a bug

I have this simple middleware:

import { Middleware, Store, Dispatch, Action } from 'redux';
import { AppState } from 'types/appState';

export default function createSimpleMiddleware(): Middleware {
  return (store: Store<AppState>) => (next: Dispatch<AppState>) => (action: Action): Action => {
    return next(action);
  };
}

It is useless, but there is a problem) This code works with typescript version 2.3.x and below. But in typescript 2.4 there is an Error.

What is the current behavior? In typescript I have this Error:

Types of parameters 'store' and 'api' are incompatible.
    Type 'MiddlewareAPI<S>' is not assignable to type 'Store<AppState>'.

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem via https://jsfiddle.net or similar.

You can find demo code at the top of current issue.

What is the expected behavior?

I expect, that there won’t be any errors.

Which versions of Redux, and which browser and OS are affected by this issue? Did this work in previous versions of Redux?

Typescript: 2.4.1 Redux: 3.7.1 NodeJS: 6.9.1 OS: MacOS

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 19 (4 by maintainers)

Commits related to this issue

Most upvoted comments

This is my workaround for now:

declare module 'redux' {
  export interface Middleware<T=any> {
    <S>(api: MiddlewareAPI<S>): (next: Dispatch<S>) => Dispatch<S>;
    (api: MiddlewareAPI<T>): (next: Dispatch<T>) => Dispatch<T>;
  }
}

@timdorr Shouldn’t we reopen this until it is fixed?

@artem-malko after typescript@2.4.x change the generics check,

Middleware should change the position ofS too.

export interface Middleware<S> {
  (api: MiddlewareAPI<S>): (next: Dispatch<S>) => Dispatch<S>;
}

Please reopen, this is not fixed yet. I still have the same error too

TypeScript: 2.4.2 Redux: 3.7.2 NodeJS: 7.7.3 OS: MacOS

Is it possible to add an example of what a valid middleware would look like with typescript? Even after following this thread, I’m unsure how to update my middleware so that it compiles correctly and types are satisfied.