Filtering an array of Maybe (nullable) types in Flow to remove null values

Viewed 2266

I have an array of Maybe (nullable) type, I want to filter those null's to have an array containing only non-null values:

@flow

type Foo = {
  foo: string
}

const bar: Array<?Foo> = [ null, { foo: 'Qux' } ]

const baz = bar
  .filter(x => x != null)
  .map(({ foo }) => foo);

However, flow complains that the argument can be still null, while it clearly cannot:

11: .map(({ foo }) => foo);
            ^ property `foo`. Property cannot be accessed on possibly null value

See the code on flow.org/try.

Is there a way to tell flow that the array includes only non-nullable items now?

This destroys my functional JavaScript programming.

1 Answers
Related