Skip to content

Cannot pass Array<T> to a function expecting Array<?T> #4251

@silverlyra

Description

@silverlyra

I just discovered some behavior regarding arrays of maybe types that I found surprising:

const lengths = (strings: Array<?string>): Array<number> =>
    strings.map((s) => s ? s.length : 0)

let strings: Array<string> = ['hello', 'world']
lengths(strings) // Flow error

An Array<T> cannot be passed as an argument for a parameter of type Array<?T>, even though all values in an Array<T> are valid in an Array<?T>. 👉 try flow

Casting via any works, but is nasty. Is this expected behavior? Is there a workaround other than casting to any?

I'm sorry if this is already a reported issue; I couldn't find it.

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