Shubham Kanodia
Shubham Kanodia

Reputation: 6236

Flow Type: Return type of the function to be same as args of a callback?

I have the following function –

function wrapper(callback) {
  // operation
}

I need the return type of wrapper to be the same as the first(only) argument passed to callback. For eg.

wrapper(({paramA, paramB}) => {}) 
// returns { paramA: somveVal, paramB: someVal }

Is that even possible?

Upvotes: 0

Views: 73

Answers (2)

Jordan Brown
Jordan Brown

Reputation: 638

function wrapper<T>(callback: T => mixed): T { ...}

Note that this signature is unimplementable unless you didn't actually want it to be generic.

Upvotes: 1

Marko Savic
Marko Savic

Reputation: 2384

If i understand well, you need something like this:

For example declare type CallBack:

type CallBack = {|
  paramA: any,
  paramB: any,
 // Or use other type instead any.
|}

And annotate function:

const wrapper = (fn: (x: CallBack) => CallBack): CallBack => ({ paramA: 1, paramB: 2 });

Flow example: Flow Try

Upvotes: 0

Related Questions