Flow: Ramda.pluck: function cannot be called on any member of intersection type

Created on 6 Dec 2017  路  2Comments  路  Source: facebook/flow

Meta

  • flow-bin 0.60.1
  • flow-typed 2.2.3
  • ramda 0.25.0 (with type def 656f69c1c8/ramda_v0.x.x/flow_>=v0.49.x)

Problem

The following code

function pluckIds<V, T: Array<{ [key: string]: V }>>(items: T) {
  return R.pluck('id', items)
}

causes some hard-to-understand errors:

  8:   return R.pluck('id', items)
              ^^^^^^^^^^^^^^^^^^^^ call of method `pluck`. Function cannot be called on any member of intersection type
  8:   return R.pluck('id', items)
              ^^^^^^^ intersection
  Member 1:
                               v
  819:   declare function pluck<
  820:     V,
  821:     K: string | number,
  ...:
  826:   ): Array<V>;
         ----------^ function type. See lib: flow-typed/npm/ramda_v0.x.x.js:819
  Error:
    8:   return R.pluck('id', items)
                              ^^^^^ array type. Has some incompatible type argument with
  822:     T: Array<Array<V> | { [key: string]: V }>
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ array type. See lib: flow-typed/npm/ramda_v0.x.x.js:822
    Type argument `T` is incompatible:
      822:     T: Array<Array<V> | { [key: string]: V }>
                        ^^^^^^^^ array type. This type is incompatible with. See lib: flow-typed/npm/ramda_v0.x.x.js:822
        7: function pluckIds<V, T: Array<{ [key: string]: V }>>(items: T) {
                                         ^^^^^^^^^^^^^^^^^^^^ object type

Isn't Array<{ [key: string]: V }> a narrower type of Array<Array<V> | { [key: string]: V }>? Why wouldn't it pass type checking?

Most helpful comment

@iwinux How you solve this problem? Have similar

All 2 comments

Oops, seems to be related to covariance / contravariance.

Ref: https://github.com/facebook/flow/issues/4258

@iwinux How you solve this problem? Have similar

Was this page helpful?
0 / 5 - 0 ratings