I saw this block of code in our code base and I have a bit of a problem to understand void = (page)
. According to , the return type is ing after =>
which is void
in my case. So what does = (page) => {}
do?
What is its equivalent function if I don't write it with fat arrow function?
This is the code:
private navigateTo: (page: string) => void = (page) => {
// display page
}
I saw this block of code in our code base and I have a bit of a problem to understand void = (page)
. According to https://stackoverflow./a/34274584/513413, the return type is ing after =>
which is void
in my case. So what does = (page) => {}
do?
What is its equivalent function if I don't write it with fat arrow function?
This is the code:
private navigateTo: (page: string) => void = (page) => {
// display page
}
Share
Improve this question
edited Aug 7, 2018 at 15:21
Philipp Meissner
5,4825 gold badges36 silver badges60 bronze badges
asked Sep 2, 2017 at 2:01
HesamHesam
53.6k78 gold badges230 silver badges374 bronze badges
2
-
= (page) => {}
says string (page) goes to this function which returns nothing (void). – Xaqron Commented Sep 2, 2017 at 2:10 - The term "fat arrow function" has been obsolete for several years. They are called simply "arrow functions". – user663031 Commented Sep 2, 2017 at 3:52
2 Answers
Reset to default 13You are looking at the code incorrectly. The general structure is
private Name: Type = Value
The type is (page: string) => void
and the value is (page) => {}
. The type means that navigateTo
is a function that accepts a string as argument and returns nothing, which is what (page) => {}
does.
In Typescript, typings are inserted inside the statements of the language, transforming them a bit.
The code you submitted should read as such:
private navigateTo
: This part is straighforward. We create a private member called navigateTo inside the current class....: (page: string) => void
: This is the type of the member. In this case, it represents a function taking in astring
parameter and not returning anything (void). This part is purely Typescript.... = (page) => { /* display page */ }
: This is the actual function being assigned to the variable.
I remend you read some of the Typescript Handbook. It has a lot of information about the syntax and the language.