Reputation: 1377
I stumble upon below code of arrow function in a book "Getting Started with Angular, Second Edition".
let isPrime: (n: number) => boolean = n => {
// body
};
I want to confirm correctness of this breakdown.
let isPrime
= function name "isPrime"(n: number)
= input parameter number "n"=> boolean
= arrow function to check boolean (a place to put the logic)=n
= i don't get this part. does this mean if i put "logic to find prime number in third step" and true, you get "n" that satisfy my logic? => {}
= i can put return or other logic here for final process. The last question is how many arrow functions is too much for chaining or curring?
I believe @Fenton give clear explanation for my understanding.
@Sebastien gives me answer that make me realise my wrong interpretation to arrow function ; equal and arrow sign doesn't always point to function, and can represent datatype too.
below is combined version of my accepted answer.
Now let's describe the types for this function, which are that is takes in a number, and gives back a boolean.
//correct usage : return boolean
let isPrime: (n: number) => boolean = n => {
// body
return true
};
//incorrect usage
let isPrime: (n: number) => boolean = n => {
// body
return "wrong"
};
I suppose that I would write it as below, unless I had a good reason to use an arrow function!
//correct usage : return boolean
function isPrime(n: number): boolean {
// body
return true;
}
//incorrect usage
function isPrime(n: number): boolean {
// body
return "wrong";
}
My final test is like this.
let isRightLogic: (n: number) => { host: boolean } = n => {
return { host: true };
}
console.log(isRightLogic(1)); // always return true but you get the idea.
Upvotes: 3
Views: 1959
Reputation: 12139
I would break down the statement in 2 parts
Part 1 - the variable type definition
let isPrime: (n: number) => boolean
is the type definition of the isPrime
variable. In this case isPrime
will contain a function:
(n: number) => boolean
which takes one argument of type number
and returns a boolean
Part 2 - the actual value assigned to the isPrime
variable (after the =
sign)
n => {
// body
};
This is the actual value, which, in accordance wth the type definition, is a function that takes one argument... but it is incomplete here as there is no return value in the body.
This would be acceptable:
let isPrime: (n: number) => boolean = n => {
return true
};
But this would violate the return type definition
let isPrime: (n: number) => boolean = n => {
return 'wrong!';
};
To answer your last question, there is no chaining here, only one variable type definition, followed by an assignment.
Upvotes: 1
Reputation: 251102
Here are some other ways of looking at it, which reveal the different perspectives.
This is the old school, just a function accepting n
which ought to be a number, and in the body somewhere it returns true or false.
let isPrime = function(n) {
// body
};
Let's use an arrow function, instead of the function keyword (there is some behaviour change here depending on the function body)...
let isPrime = (n) => {
// body
};
We technically don't need those brackets (although I think readability suffers without them:
let isPrime = n => {
// body
};
Now let's describe the types for this function, which are that is takes in a number, and gives back a boolean.
let isPrime: (n: number) => boolean = n => {
// body
};
I suppose that I would write it as below, unless I had a good reason to use an arrow function!
function isPrime(n: number): boolean {
// body
}
Upvotes: 5
Reputation: 413846
In the code you posted, (n: number) => boolean
is the type signature for the function being created. The function itself is just
n => {
// body
}
The n
is the parameter name, as indicated by the type signature. Thus Typescript knows that parameters passed to the function should be numbers, and that the return value from the function will be true
or false
.
The symbol isPrime
is not really the function name in a formal sense; it's a variable whose value happens to be that function.
In plain JavaScript, the variable declaration would be juss
let isPrime = n => {
// body
};
Upvotes: 6