函数参数的 this 关键字

this keyword for function parameter

最近使用Rxjs 5时,我通过npm install Rxjs@5.0.1下载了Rxjs,从node_modules下下载的代码,我在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);

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

您可以(自打字稿 2.0 版起)指定调用函数时您期望的 this 是什么。

Specifying the type of this for functions所述:

Following up on specifying the type of this in a class or an interface, functions and methods can now declare the type of this they expect.

By default the type of this inside a function is any. Starting with TypeScript 2.0, you can provide an explicit this parameter. this parameters are fake parameters that come first in the parameter list of a function

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