flow-typed icon indicating copy to clipboard operation
flow-typed copied to clipboard

The type annotation of Lodash's flatMap is less permissive than its implementation

Open evankroske opened this issue 7 years ago • 4 comments

flatMap itself accepts iteratees that have less than 3 parameters, but if you pass an iteratee that only takes 2 parameters, Flow throws a type error:

_.flatMap([], (e, i) => []);
Cannot call _.flatMap because array literal [1] is incompatible with object type [2] in type argument T.

     app/components/AssignWidget.jsx
      32│   resetErrorMessages: Function
      33│ |}> {
      34│   selectedTasks = () => {
 [1]  35│     _.flatMap([1], (e, i) => []);
      36│     return _.flatMap(
      37│       this.props.isTaskAssignedToUserSelected[this.props.previousAssigneeId] || [],
      38│       (selected, id) => (selected ? [this.props.tasks[id]] : []));

     flow-typed/npm/lodash_v4.x.x.js
 [2] 557│     flatMap<T: Object, U>(

I think we could fix this by making the parameters of FlatMapIteratee optional.

evankroske avatar Jul 03 '18 22:07 evankroske

Seems reasonable, go for it.

AndrewSouthpaw avatar Jul 13 '18 18:07 AndrewSouthpaw

If you look at the error message, I don't think it's the number of arguments; it's the fact that the first argument can be an array or an object.

Do I mind if I take this @evankroske ? This is blocking another project.

CAYdenberg avatar Aug 01 '18 22:08 CAYdenberg

@CAYdenberg, go for it! I haven't had a chance to work on this. I think you're right that the number of arguments of the callback isn't the issue.

evankroske avatar Aug 02 '18 18:08 evankroske

Any news? 😞 Got this error:

Cannot call _.flatMap because array type [1] is incompatible with object type [2] in type argument T.

At this code:

const result = _.flatMap(order, id =>
  _.get(items, [id, 'otherOrder'], [])
);

nickensoul avatar May 21 '19 23:05 nickensoul