Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Signature of lambda return types in TypeScript

In the TypeScript below both functions are the same, except that I'm trying to explicitly declare the return type in demoTwo. The return type is a function which itself takes a function as input. My question is why do I have to give the parameter name represented by whyThis, given that it will never be used? The code will not compile without something in that position.

function demoOne() {
    return function(input: () => string) : void {
        var result = input();
        console.log("Foo:",result);
    }
}

function demoTwo(): (whyThis:() => string) => void {
    return function(input: () => string) : void {
        var result = input();
        console.log("Bar:",result);
    }
}

var sampleInput = () => "wibble";
demoOne()(sampleInput);
demoTwo()(sampleInput);

To be clear what I'm asking here's the equivalent code in Scala:

object Program {
  def demoTwo(): (() => String) => Unit = {
    def tmp(input: () => String): Unit = {
      val result = input()
      println("Bar: " + result)
    }
    return tmp
  }
  def main(args: Array[String]): Unit = {
    val sampleInput = () => "wibble"
    demoTwo()(sampleInput)
  }
}

If we set the declarations of demoTwo side by side we have:

function demoTwo(): (whyThis:() => string) => void { //TS
def demoTwo(): (() => String) => Unit = { //Scala

The only major difference is that TS requires something at the whyThis position and Scala does not. Why should this be the case?

like image 613
Garth Gilmour Avatar asked Aug 13 '17 11:08

Garth Gilmour


People also ask

What is the return type of lambda?

The return type for a lambda is specified using a C++ feature named 'trailing return type'. This specification is optional. Without the trailing return type, the return type of the underlying function is effectively 'auto', and it is deduced from the type of the expressions in the body's return statements.

How do I specify a return type of function in TypeScript?

To define the return type for the function, we have to use the ':' symbol just after the parameter of the function and before the body of the function in TypeScript. The function body's return value should match with the function return type; otherwise, we will have a compile-time error in our code.

What => means in TypeScript?

ES6 version of TypeScript provides an arrow function which is the shorthand syntax for defining the anonymous function, i.e., for function expressions. It omits the function keyword. We can call it fat arrow (because -> is a thin arrow and => is a "fat" arrow). It is also called a Lambda function.

What is call signature in TypeScript?

In TypeScript we can express its type as: ( a : number , b : number ) => number. This is TypeScript's syntax for a function's type, or call signature (also called a type signature). You'll notice it looks remarkably similar to an arrow function—this is intentional!


1 Answers

I think the confusion stems from the way the signatures are defined. Note the 3 signatures below

(number) => Void
(x : number) => Void
(_ : number) => Void

The last two are signatures of void functions that take an argument of type number. The first however is a function that takes an argumnet of type Any, with a name number. So the language requires a name followed by a type, and if the type is omitted then the Any type is assumed.

So,

(() => string) => void

Is not valid as '() => string' is not a valid name and the compiler is getting confused trying to parse it as such.

Shortest form will be using '_ : type'.

But I agree it would have been nicer for the name to be omitted and only type being used or the name being optional. I'm not sure why they made that design decision, perhaps to avoid confusion with existing JavaScript arrow functions (without types).

like image 137
Eamonn Boyle Avatar answered Oct 25 '22 21:10

Eamonn Boyle