Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the return type for a function that returns another function

I am working on developing Protractor tests using Typescript. It appears that the d.ts file available for protractor is very out of date. I am trying to update it to include the Expected Conditions protractor has added.

To summarize it, Expected Conditions are a set of functions within protractor that return a function that returns a promise of your value.

An example of usage:

protractor.ExpectedCondtions.visibilityOf(element(by.id('button1')))(); 

I am stumped on how to tell protractor that I am returning a function that will return a specific return type. Does anyone have any experience with this?

like image 330
jordan Avatar asked Nov 25 '15 17:11

jordan


People also ask

Can I return another function in Python?

Python may return functions from functions, store functions in collections such as lists and generally treat them as you would any variable or object. Defining functions in other functions and returning functions are all possible.

What is it called when a function returns a function?

A return is a value that a function returns to the calling script or function when it completes its task. A return value can be any one of the four variable types: handle, integer, object, or string.

Can you return a function in another function?

A function is an instance of the Object type. You can store the function in a variable. You can pass the function as a parameter to another function. You can return the function from a function.

What is the return type of a function?

The result of a function is called its return value and the data type of the return value is called the return type. If a function declaration does not specify a return type, the compiler assumes an implicit return type of int .


2 Answers

If I understood you correctly, your solution will depend on the type that the "second" function returns.

In a nutshell, there are at least 2 ways to do it:

  1. Lambda syntax
  2. Interfaces (normal and generic interfaces)

I've tried to explain all these in the code below, please, check it:

module main {     export class TestClass     {         // Use lamba syntax as an interface for a return function         protected returnSpecificFunctionWhichReturnsNumber(): () => number         {             return this.specificFunctionWhichReturnsNumber;         }          protected specificFunctionWhichReturnsNumber(): number         {             return 0;         }          // Use an interface to describe a return function         protected returnSpecificInterfaceFunction(): INumberFunction         {             return this.specificFunctionWhichReturnsNumber;         }          // Use a generic interface to describe a return function         protected returnSpecificGenericInterfaceFunction(): IReturnFunction<number>         {             return this.specificFunctionWhichReturnsNumber;         }     }      // An interface for a function, which returns a number     export interface INumberFunction     {         (): number;     }      // A generic interface for a function, which returns something     export interface IReturnFunction<ValueType>     {         (): ValueType;     } } 
like image 55
Mark Dolbyrev Avatar answered Sep 21 '22 06:09

Mark Dolbyrev


Since this question popped up first in Google for how to type return function for a function that returns a function, I will add the generic solution here for declaring these types.

So if you want to add type declaration to this curried add function:

const add = (a : number) => (b: number) => a + b; 

You just duplicate what is after the = sign and make the return value the corresponding value:

export const add: (a : number) => (b: number) => number =     (a : number) => (b: number) => a + b; 

But at this point, you don't need the types for the actual function, so you can just type this, as if it was JS:

export const add: (a : number) => (b: number) => number =     a => b => a + b; 

Writing it more extensively:

const add: (a : number) => (b: number) => number =     a => {         console.log(a);         return b => {             console.log(b);             return a + b;         }     }; 

With generics:

export const add: <A extends number, B extends number>(a : A) => (b: B) => number =     a => b => a + b; 
like image 29
andras Avatar answered Sep 20 '22 06:09

andras