I want to get the state with type checking in my redux middleware. Using Typescript 2.6.2, I can readily create a redux middleware as follows:
import { Middleware, MiddlewareAPI, Dispatch } from 'redux';
import { IState } from '~/shared/rootReducer';
const logger: Middleware =
<S>({ getState }: MiddlewareAPI<S>) => (next: Dispatch<S>) => (action: any) => {
console.log(action);
next(action);
};
export default logger;
I'd like to do const { songs } = getState(); with typechecking, but I can't infer the type of S to be of IState, which is the type of my root reducer. Attempting to do const logger: Middleware = <S extends IState> gives me this error:
Type 'S' is not assignable to type 'IState'.
I've also tried creating a method that returns state is IState, but that failed as well. And I could do this:
const state = <IState><any>getState();
But I really rather not.