flow icon indicating copy to clipboard operation
flow copied to clipboard

Method overloading does not work in interface files if one of the types involves a $Shape

Open pasha-mf opened this issue 11 years ago • 2 comments

Consider the following test case, consisting of one interface file and one script file.

/* interface file */
declare class Lodash {
  find<T>(collection: Array<T>, iterator: (value: T, i?: number) => boolean, thisArg?: any): ?T;
  find<T>(collection: Array<T>, value: $Shape<T>): ?T;
}
declare var _: Lodash;

/* script */
type User = { id: number; name: string; };
function test (users: Array<User>) {
  _.find(users, (user) => user.id===4);
  _.find(users, {name: "zuck"});
}

The second _.find produces an error "call of method find: Function cannot be called on [LIB] interface.js: Lodash". If I put declarations into the same file as the script, everything will work. Also if I replace $Shape<T> with Object it will also work. However neither of these workarounds are desirable.

pasha-mf avatar Sep 11 '15 21:09 pasha-mf

If you switch the order of the find declarations, so the $Shape one is first, then this starts working for me. I think the root cause is the same bug from #583. Since objects can be called like functions (callable object types), Flow starts exploring whether { name: "zuck" } matches the iterator function from the first option in the find overload/intersection type, fails in some nested speculative match, which fails the whole thing.

samwgoldman avatar Sep 11 '15 21:09 samwgoldman

This is also because $Shape<T> will always match a function type since functions have an underlying object type with no properties. This will be fixed in an upcoming release 👍

calebmer avatar Jul 19 '17 20:07 calebmer