title: Creating Redux Middleware with Typescript
body: <p>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:</p>
  
  <pre><code>import { Middleware, MiddlewareAPI, Dispatch } from 'redux';
  import { IState } from '~/shared/rootReducer';
  
  const logger: Middleware = 
      &lt;S&gt;({ getState }: MiddlewareAPI&lt;S&gt;) =&gt; (next: Dispatch&lt;S&gt;) =&gt; (action: any) =&gt; {
      console.log(action);
      next(action);
  };
  
  export default logger;
  </code></pre>
  
  <p>I'd like to do <code>const { songs } = getState();</code> with typechecking, but I can't infer the type of <code>S</code> to be of <code>IState</code>, which is the type of my root reducer. Attempting to do <code>const logger: Middleware = &lt;S extends IState&gt;</code> gives me this error:</p>
  
  <p><code>Type 'S' is not assignable to type 'IState'.</code></p>
  
  <p>I've also tried creating a method that returns <code>state is IState</code>, but that failed as well. And I could do this:</p>
  
  <pre><code>const state = &lt;IState&gt;&lt;any&gt;getState();
  </code></pre>
  
  <p>But I really rather not.</p>