函数参数

时间:2017-05-24 16:34:20

标签: typescript rxjs5

最近当我使用Rxjs 5时,我使用npm install Rxjs@5.0.1下载了Rxjs,从node_modules下的代码下载,我在Rxjs文件夹中找到了Observable.d.ts,我看到它声明它的构造函数如下: / p>

 *
 * @constructor
 * @param {Function} subscribe the function that is  called when the Observable is
 * initially subscribed to. This function is given a Subscriber, to which new values
 * can be `next`ed, or an `error` method can be called to raise an error, or
 * `complete` can be called to notify of a successful completion.
 */
constructor(subscribe?: <R>(this: Observable<T>, subscriber: Subscriber<R>) => TeardownLogic);

我的问题是:在subscribe的函数类型声明中这个关键字的用法是什么?:(这个:Observable,...),TypeScript是否有关于这个关键字用法的一些文档?谢谢。

1 个答案:

答案 0 :(得分:4)

您可以(从打字稿的2.0版开始)指定在调用函数时您期望的this

Specifying the type of this for functions中所述:

  

跟进指定类或类中的类型   接口,函数和方法现在可以声明它们的类型   期望的。

     

默认情况下,函数内部的类型是any。从...开始   在TypeScript 2.0中,您可以提供一个显式的this参数。这个   参数是参数列表中首先出现的伪参数   功能

请注意,这不会被翻译成js,因此它不是函数中的真实参数。