简体   繁体   English

Typescript ES6箭头语法

[英]Typescript ES6 arrow syntax

What is difference between canDeactivate1 & canDeactivate2 in below typescript interface ? 以下打字稿界面中的canDeactivate1和canDeactivate2有什么区别? How these functions will be considered ? 如何考虑这些功能?

export interface CanComponentDeactivate
{
    canDeactivate1 : ()=> Observable<boolean> | Promise<boolean> | boolean;
    canDeactivate2 (): Observable<boolean> | Promise<boolean> | boolean;
}

How can i write same function in JavaScript ? 如何使用JavaScript编写相同的函数?

The second is basically a shortcut (well, not much) for the first. 第二个基本上是第一个的快捷方式(不是很多)。

The expression ()=> Observable<boolean> | Promise<boolean> | boolean 表达式()=> Observable<boolean> | Promise<boolean> | boolean ()=> Observable<boolean> | Promise<boolean> | boolean ()=> Observable<boolean> | Promise<boolean> | boolean is a type declaration for a function that expects no arguments and returns an observable, a promise or a boolean. ()=> Observable<boolean> | Promise<boolean> | boolean是函数的类型声明,该函数不要求任何参数,并且返回可观察的,应许的或布尔值。 You can use such declarations anywhere a type is expected: 您可以在需要类型的任何地方使用此类声明:

let foo: ()=> Observable<boolean> | Promise<boolean> | boolean;
foo = 1; //doesn't work, foo has to be a function
foo = () => 1; //doesn't work, we are not allowed to return a number
foo = () => true; // works
foo = function() { return true }; //works

Because functions on objects are usually used as methods, interfaces have a special syntax that resembles method declarations in classes. 因为对象上的函数通常用作方法,所以接口具有类似于类中方法声明的特殊语法。 canDeactivate2 (): Observable<boolean> | Promise<boolean> | boolean canDeactivate2 (): Observable<boolean> | Promise<boolean> | boolean looks like a method without a body, which is more in line with what other languages have. canDeactivate2 (): Observable<boolean> | Promise<boolean> | boolean看起来像没有主体的方法,这与其他语言的语言更加一致。

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

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