Ruth
Ruth

Reputation: 634

Optional object properties in Flow

Flow version: v0.114.0

type Arg = Array<{
  someProp: string,
  maybeOptional?: string
}>

const someFunc = (arg: Arg) => {
  return arg
}


type OtherArg = Array<{
  someProp: string,
  maybeOptional: string
}>

const otherFunc = (otherArg: OtherArg) => {
    return someFunc(otherArg)
}

Expected behavior

When an optional field is provided an error should not be raised. Use case: Generic component that optionally performs certain functionality - A particular implementation of that generic component that we always expect to have that functionality

Actual behavior

Cannot call someFunc with otherArg bound to arg because string [1] is incompatible with undefined [2] in property maybeOptional of array element.

https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAodAXAngBwKZgCCATgOZgC8RxxAhlgDwDeqYYAznALZ4AKxcHAC4OGYgEsAdqQA0rMF3oAjPAHkcGcXEm0YAfhHsxU0qgC+APnQBjbUY7c8AMQCuk65TAAKWmRElSAEpKCzAWNmI8DBdiSTBfUzN0VGx8MFUMAAs8YgDPEjpGcIcefkFDY2k5NkUsFXVNbV0KiWlzK1RbSXs4LJzXd08vXuzcv3S+saCQsNQASEjo2JLnN2thyYDA83QgA

Upvotes: 0

Views: 157

Answers (1)

Ruth
Ruth

Reputation: 634

Answered in an issue on the Flow repo:

I recommend to always use at least $ReadOnlyArray instead of Array.

https://github.com/facebook/flow/issues/8238#issuecomment-566583654

Upvotes: 1

Related Questions