8000 Flow: List.filter(Boolean) should refine item type to $NonMaybeType · Issue #1200 · immutable-js/immutable-js · GitHub
[go: up one dir, main page]

Skip to content
Flow: List.filter(Boolean) should refine item type to $NonMaybeType #1200
Closed
@wokalski

Description

@wokalski

This currently throws a flow error

const list = List.of(1, null);
const nonMaybeList: List<number> = list.filter(Boolean);

It is implemented in Array in core. (Although the implementation is not ideal)

For immutable it can be fixed with appropriate overrides for all types. For instance, for List<T> the override should look like this.

  filter(
    predicate: typeof Boolean
  ): List<$NonMaybeType<T>>;

This works but causes the following flow error:

node_modules/immutable/dist/immutable.js.flow:464
464:   ): List<$NonMaybeType<T>>;
          ^^^^^^^^^^^^^^^^^^^^^^ List. This type is incompatible with
116:   ): this;
          ^^^^ some incompatible instantiation of `this`

In order to resolve it, we have to replace this from the superclass implementation with _Iterable<K, $NonMaybeType<V>, KI, II, SI> and then insert overrides for all concrete types analogically to the List override. The same is done for .map method. Those workarounds exist because of flow limitations in handling generic class hierarchy.

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

      0