0

I have been playing with Observables recently, and I came across this constructor definition of the Observable

constructor(subscribe?: (this: Observable<T>, subscriber: Subscriber<T>) => TeardownLogic) {

so it says, that constructor accepts as an argument a callback function that should accept 2 arguments and return TeardownLogic

However, when I try to follow that restriction, I have an error, and only single argument function is accepted.

What is special about this: Observable? What does that actually mean in constructor context?.

As example:

///  this is invalid, but it is matching the cosntructor definition
  new Observable(function (x, y) {

  })
//// this is valid
  new Observable(function (x) {
  
  })
Antoniossss
  • 31,590
  • 6
  • 57
  • 99
  • 1
    "*a callback function that should accept 2 arguments*" it accepts one argument and [declares what the type of `this` would be](https://stackoverflow.com/questions/28920753/declaring-the-type-of-this-in-a-typescript-function). See also [the documentation](https://www.typescriptlang.org/docs/handbook/2/functions.html#declaring-this-in-a-function) – VLAZ Jun 03 '21 at 19:53
  • Ye this is the point where I think that I don;t know what is happening and `this` is not a coincidence. – Antoniossss Jun 03 '21 at 19:54
  • 1
    Nothing is happening. There is TS syntax for declaring the type of `this`. That's *not* a parameter. The second item is a parameter. So, the callback takes one parameter and the type of `this` it would be called with is also declared. – VLAZ Jun 03 '21 at 19:55
  • This is something that i didn't know (was supsecting obviously) therfore, to someone that does not know that fake param syntax, it looks like 2 args function. Thanks for the answer. – Antoniossss Jun 03 '21 at 20:02
  • Example usage (which is kind of cool) https://www.typescriptlang.org/play?ssl=14&ssc=1&pln=1&pc=1#code/MYGwhgzhAEBiD28DeAoa0AOBXARiAlsNAOYBOApuQC74B2xEAvAEQAS5II80zA3CgF8UKUJBgAhMKVTpBw4PFoQq0CGACe7TvEYAzLLWA1FACioALfBABcCeABoAthGLXlpOsQCUM6AqXwIOQAdFzEZpYQwWSUNPQQANTO3vxCapocXME4dAAmJrTkAO5wiCZeXibMAMrkOJA0YLTMXvzpWlk5tPmFJZKk5ZU1dQ34TS38QA – Antoniossss Jun 03 '21 at 20:08

0 Answers0