Search code examples
typescriptconstructornew-operator

typescript constructor new object


What is the meaning of the second parameter in the constructor:

(from this example)

constructor(private url: string, private WebSocketCtor: { new(url:string): WebSocket } = WebSocket) {}

Especially the = WebSocket part at the end. Why do I need this? In the example above it is called like this:

bootstrap(AimApp, [
  ...,
  provide(RxWebSocket, {useFactory: (url:string) => {
    return new RxWebSocket(url, WebSocket);
  }, deps: [SOCKET_URL]})
]);

Since I am not very familiar with Angular, how would you call it otherwise?


Solution

  • { new(url:string): WebSocket } this defines a constructor signature, which is used to hold class constructors. = WebSocket provides a default value for the parameters, the WebSocket class.

    The point of this is to allow the user to plug in a custom WebSocket class, which is compatible with the WebSocket class, but also provides a default implementation by providing a default value for the WebSocketCtor parameter