Skip to content

Inferred array type allows unsound covariance? #8607

@jtbandes

Description

@jtbandes

The following code should raise an error, but doesn't:

Try Flow

const a = [1];

function foo(arr: (number|string)[]) {
    arr.push("x");
}
foo(a);

function bar(arr: number[]) { }
bar(a);

This problem seems to be specific to arrays. If I replace all T[] with {x: T}, then I get the expected behavior.

Flow version: v0.146.0

Expected behavior

Either an error on foo(a), or an error on bar(a). If a is inferred to be number[], then foo(a) should be an error. If it's inferred to be (number|string)[], then bar(a) should be an error.

Actual behavior

When the type of the array is left off (inferred), it seems to produce unsound results. When the type is explicitly specified I get the expected results.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Typing: soundnessNo false negatives (type checker claims that there is no error in the incorrect program)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions