2

I've just started learning typescript recently and I find it incredibly difficult to understand the syntax. The notes I'm provided with weren't sufficient to clear up my understanding so I would like some assistance on it.

In my notes, I have two typescript functions:

const operationOnTwoNumbers =
   (f:(x:number, y:number) => number) =>   // I don't understand this line 
       (x:number) =>
           (y:number) =>
               f(x,y)

function operationOnTwoNumbers(f:(x:number, y:number) => number) {
   return function(x:number) {
       return function(y:number) {
          return f(x,y);
       }
   }
}

I've added a comment in the code above. Could anyone please explain in simple terms on how I interpret (f:(x:number,y:number) => number)?

And then I also have:

type BinaryNumberFunc = (x:number, y:number) => number
type CurriedNumberFunc = (x:number) => (y:number) => number

const operationOnTwoNumbers: (f:BinaryNumberFunc) => CurriedNumberFunc 
       = f => x => y => f(x,y)

function operationOnTwoNumbers(f:BinaryNumberFunc):CurriedNumberFunc {
   return function(x) {
       return function(y) {
           return f(x,y)
       }
   }
}

I know that both operationOnTwoNumbers function are similar in terms of functionality, but how do I interpret those functions and distinguish them?

The way I'm interpreting is:

operationOnTwoNumbers is a function that takes in a function as a parameter of type BinaryNumbeFunc and operationOnTwoNumbers is of type CurriedNumberFunc that takes in f as an argument and returns a function x, and returns a function y, and performs an action of f on x and y.

I'm totally lost trying to understand typescript and would like some help to improve my understanding of the syntax.

icc97
  • 11,395
  • 8
  • 76
  • 90
Maxxx
  • 3,688
  • 6
  • 28
  • 55
  • 2
    Possible duplicate of [What is 'Currying'?](https://stackoverflow.com/questions/36314/what-is-currying). This is functional programming knowledge, you can use on JavaScript, TypeScript or any language that support it – hgiasac Aug 10 '18 at 03:10
  • (f:(x:number,y:number) => number) this line mean the TYPE of "f" is a TUPLE like in C# that mean this variable hold a Type which are made up of two further variable which are of number Type – Irshad Ahmed Akhonzada Aug 16 '18 at 08:20

0 Answers0