This class is for overriding the parenthesis operator "()" of a class.

Javascript / typescript don't support overriding operators. But you can use a trick by creating a function when 'new' is called and making the original class a prototype of the returned function.

Deriving from 'Function' is not really required, but with it you get all the members of a function object.

Note: This redefines the prototype of a function with setPrototypeOf. According to MDN this might come with severe speed penalties, because this will disable some optimizations in certain runtimes.

Q: How does this thing work?
A: Read about prototype inheritance, then you might understand the explanation at the top of this comment. Also the first link will show you how it is done in javascript which is the same.

Q: Why is the functor interface needed?
A: Because it extends the type of the class in a way that the class is considered callable by typescript. It does not make it callable - that is achieved by the setPrototypeOf call in the class constructor

Q: What is the single unnamed parameter in the functor interface?
A: That the interface is directly callable

Type Parameters

  • T extends ((...arg: any) => any)

Hierarchy (view full)

  • Parameters

    • Rest...args: Parameters<T>

    Returns ReturnType<T>

Constructors

Constructors

  • Constructs the callable class.

    Type Parameters

    • T extends ((...arg: any) => any)

    Parameters

    • f: ((...args: Parameters<T>) => any)

      The function that is invoked when the () operator is invoked. If you use an arrow function in the derived class you can safely use 'this'.

        • (...args): any
        • Parameters

          • Rest...args: Parameters<T>

          Returns any

    Returns Functor<T>