4

I've come across the following type annotation on a function parameter:

typeFilter : new(...args) => T

where T is a generic parameter on the function.

What does new(...args) mean in this context, and where is it documented?

Zev Spitz
  • 13,950
  • 6
  • 64
  • 136
  • 1
    It defines a constructor type, something where `new typefilter(...)` would produce an instance of `T`. See e.g. https://stackoverflow.com/questions/38311672/generic-and-typeof-t-in-the-parameters/38311757#38311757, https://stackoverflow.com/q/13407036/3001761 – jonrsharpe Feb 11 '18 at 12:56
  • 1
    And `...args` - [rest parameters](https://www.typescriptlang.org/docs/handbook/functions.html#rest-parameters). To sum up - constructor of `T` taking any parameters – Aleksey L. Feb 11 '18 at 12:58
  • @jonrsharpe I was hoping for a link to the official documentation. – Zev Spitz Feb 11 '18 at 13:54
  • 1
    @AlekseyL. Can you post this as an answer? – Zev Spitz Feb 11 '18 at 13:55

1 Answers1

7

TL;DR new(...args) => T represents constructor of T taking any parameters.

new describes "static" part of a class/function, meaning it is a constructor and consumer can create new instance of T using new keyword. Example here.

As for ...args - these are rest parameters

Rest parameters are treated as a boundless number of optional parameters. When passing arguments for a rest parameter, you can use as many as you want; you can even pass none

Aleksey L.
  • 35,047
  • 10
  • 74
  • 84
  • Just as a note: A technique called mixins (something like polymorphism) use such annotations. – rekire Aug 04 '19 at 17:55