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.