Method overloading does not work in interface files if one of the types involves a $Shape
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.
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.
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 👍