Skip to content

Generic is not resolving properly in complex situation. #5419

@kiddkai

Description

@kiddkai

Hi,

I am trying to create a "middleware" api, in redux, which already has a bunch of built-in types. And I am trying to use existing types to create a new middleware. But flow is throwing this error to me:

store.dispatch({ type: 'string' });
                           ^ object literal. This type is incompatible with the expected param type of
14:     return (store: MiddlewareAPI<S, A>) => next => (action: A) => {
                                        ^ some incompatible instantiation of `A`

When I am trying to do:

/* @flow */

declare type DispatchAPI<A> = (action: A) => A;
declare type Dispatch<A: { type: $Subtype<string> }> = DispatchAPI<A>;
declare type MiddlewareAPI<S, A, D = Dispatch<A>> = {
    dispatch: D;
    getState(): S;
};
type Middleware<S, A, D = Dispatch<A>> =
    (api: MiddlewareAPI<S, A, D>) =>
      (next: D) => D;

export const createSSRMiddleware = <S, A: { type: string }>(): Middleware<S, A> => {
    return (store: MiddlewareAPI<S, A>) => next => (action: A) => {
        store.dispatch({ type: 'string' });
        return next(action);
    };
};

Below is the link to reproduce the issue:

https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVATApgYxgQwCcswAXATwAcSARASwGdL9ScALAQQAUBJAHg4A+MAF4wACnw5SdOADsAXGA4BKUcI4BuTLgLEyVWo2as2ApQG8D1JQBIAygFcARhWp8GpQnTkBzYQC+wmL0TCzs3PxC2th4RCRuJACydBgYMFgI8ZF89gA0ygU0omChJuwCgsFgFqhg9WAYxuFsSjTaDWC+WKT2pCxY4ipK9toB2olgKWkZWcS5BRxFJWUtlcF1DZKUdErT6ZnZvAuFpYJqIoKbneJyWAAepG0Xwu3oD5RwhKRgOPKev2IA3s9gASvtZvESicOJZrFglJ5vH4wEEhntUgc5lgYcFhLVOsRSI5CHIJJ4vgippjIcQcvllOd1GA7o9mZJpLJFMoXjVrp0wBTiAA6JphUziKyJJQAciRPl8MtRKg6AvqRJJZNZpA5MnkKv541QRqAA

I also found out that even with a simple case like:

/* @flow */
const foo = <A: number>() => {
  const bar: A = 1;
}

It also producing the same error, I don't know if they are related. But looks like it's the case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions