Skip to content

in keyword doesn't seem to narrow types #5717

Closed
@massimonewsuk

Description

@massimonewsuk

Try pasting the following code into https://flow.org/try/

interface Foo {
    kids: any[];
}
interface Bar {
    category: "bar";
    children: any[];
}
interface Baz {
    category: "baz";
    minions: any[];
}

for (let item of ([] : Array<Foo | Bar | Baz>)) {
    if ("category" in item) {
       	// expecting item to be Bar | Baz here, or at least something with the property "category"
        item.category
    }
}

This code example works as expected in the TypeScript playground (provided you change the casting of the array literal using : to as, as TS uses different syntax for casting)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions