Flow: Array of a type is seen as incompatible with an Array of superset Union

Created on 10 Apr 2017  路  1Comment  路  Source: facebook/flow

I believe that in the following example, the variable a should be compatible with the parameter arg, seeing as it is a compatible subset of the union.

type FooUnion = string | { id: string };

function foo (arg: FooUnion[]) {
    console.log(arg);
}

const a: string[] = ['a'];

foo(a);
FooUnion (object type) This type is incompatible with const a: string[] = ['a'];

Most helpful comment

Problem due to variance. Use this instead:

type FooUnion = string | { id: string };

function foo (arg: $ReadOnlyArray<FooUnion>) {
    console.log(arg);
}

const a: string[] = ['a'];

foo(a);

>All comments

Problem due to variance. Use this instead:

type FooUnion = string | { id: string };

function foo (arg: $ReadOnlyArray<FooUnion>) {
    console.log(arg);
}

const a: string[] = ['a'];

foo(a);
Was this page helpful?
0 / 5 - 0 ratings