最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What's the meaning of "void = () => {}" after Fat arrow function in TypeScript? -

programmeradmin7浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 13

You 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 a string 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.

发布评论

评论列表(0)

  1. 暂无评论