@jimka/typescript-ui / core / callable
Function: callable()
ts
function callable<T>(Cls: T): Callable<T>Defined in: src/typescript/lib/core/Callable.ts:37
Wraps a class so it can be invoked without new. The returned value still works with new, with instanceof, and as the right-hand side of extends.
Type Parameters
• T extends (...args: any[]) => any
Parameters
Cls
T
The class constructor to wrap.
Returns
Callable<T>
A Proxy that forwards both [[Call]] and [[Construct]] to Cls.
Remarks
instance.constructor continues to point at the original class because the Proxy forwards [[Construct]] via Reflect.construct, and instanceof resolves through the default Function.prototype[Symbol.hasInstance] which walks the prototype chain.
Example
typescript
class Foo { constructor(public x: number) {} }
const FooCallable = callable(Foo);
const a = FooCallable(1); // no `new`
const b = new FooCallable(2); // still works
a instanceof FooCallable; // true