简体   繁体   English

打字稿语法:这到底是什么?

[英]Typescript syntax: What the heck is this?

I'm a JS guy looking at an object in Typescript (it's being used in my work project) and I've stumbled into this syntax: 我是一个看着Typescript中的对象的JS人(它正在我的工作项目中使用),我偶然发现了这种语法:

myClassFunction: () => void = () => {
    // my function internals here
}

I'm... honestly not sure what to make of this. 我......老实说不知道该怎么做。 What is this syntax, what does it do, what does it mean, and what is it called? 这是什么语法,它做什么,它意味着什么,它叫什么? (To my eyes, it looks as though we're assigning an anonymous function to another anonymous function, which shouldn't happen, but void is a TS type, so.....I'm stumped.) (在我看来,它看起来好像我们正在为另一个匿名函数分配一个匿名函数,这不应该发生,但是void是TS类型,所以......我很难过。)

Thanks! 谢谢!

There are two parts to that: 这有两个部分:

a type annotation: 类型注释:

: () => void

This means its a function that takes nothing and returns nothing 这意味着它的功能不带任何东西,也不会返回任何东西

fat arrow function 胖箭功能

() => {
    // my function internals here
}

This is an ES6 fat arrow function. 这是ES6胖箭头功能。 More on these here : http://basarat.gitbooks.io/typescript/content/docs/arrow-functions.html 更多关于这些: http//basarat.gitbooks.io/typescript/content/docs/arrow-functions.html

Opinion 意见

Personally I wouldn't annotate this. 我个人不会注释这个。

var myClassFunction = () => {
    // my function internals here
}

And just let typescript infer the type: 只需让打字稿推断出类型:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM