此关键字代表功能参数

Icy*_*Brk 3 typescript rxjs5

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

 *
 * @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);
Run Code Online (Sandbox Code Playgroud)

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

Nit*_*mer 5

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

指定函数的类型中所述:

在类或接口中指定此类型之后,函数和方法现在可以声明其期望的类型。

默认情况下,函数内部的类型为any。从TypeScript 2.0开始,您可以提供一个明确的this参数。此参数是伪造的参数,该伪造的参数首先出现在函数的参数列表中

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