4

I found the following method's signature

export function retry<T>(this: Observable<T>, count: number = -1): Observable<T> {
  return higherOrder(count)(this) as Observable<T>;
}

Where the first parameter is this and typed Observable<T>. While having this into the arguments seems to be wrong, the compiler accepts this syntax and I saw this pattern a few times already. Could someone explains what is its purpose?

Flavien Volken
  • 19,196
  • 12
  • 100
  • 133

1 Answers1

2

From the docs:

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:

function f(this: void) {
    // make sure `this` is unusable in this standalone function
}
Flavien Volken
  • 19,196
  • 12
  • 100
  • 133