Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

type parameter `U` of call of method `then`. Missing annotation

Tags:

flowtype

I have an object which might contain a promise property declared thus:

type PromiseAction = {
  +type: string,
  promise: ?Promise<any>,
};

The action argument to a function is declared to be of type PromiseAction:

(action: PromiseAction) =>

Later on I check whether the received action object does have a promise property and if action.promise has a then:

if (action.promise && typeof action.promise.then === 'function') {

If it does then I hook onto the promise chain:

return promise.then(

At which point I get the error: "type parameter U of call of method then. Missing annotation"

I can see in the source for flow that the then property of a Promise has a U parameter which, I assume, is the one being asked for.

How can an provide that U annotation if I only have only one parameter Promise<+R> in the type declaration?

like image 202
Devasatyam Avatar asked Oct 12 '17 14:10

Devasatyam


1 Answers

You do not need to define the value of U.

The flow source you linked to means, essentially, "Promises returned by then fulfill with a value that is the same as either the return value of the handlers, or the fulfilled value of the returned Promise of those handlers." That sounds confusing (because Promises can be very confusing) but the bottom line is that it's not something you "fill out". It creates a relationship between the types that then returns and the types of the return values of onFulfill and onReject passed to then.

The error you're getting means that Flow can't figure out what that relationship is because it doesn't have enough information. Annotate the then callbacks with types:

return promise.then((a:string)=>...)

That will either fix the error, or at least disambiguate U enough to give you a more specific error.

like image 115
EugeneZ Avatar answered Nov 01 '22 02:11

EugeneZ