A TypeScript utility that transforms a class instance into a callable function using Proxy. Allows you to invoke an instance as a function while maintaining access to all its properties and methods.
Use Case
Perfect for creating fluent APIs where you want an object to be both callable and maintain its class interface:
classCalculator {
value:number=0
call(x:number) {
this.value += x
returnthis
}
reset() {
this.value =0
returnthis
}
}
constcalc=toCallable(newCalculator())
calc(5)(10) // Callable via call() method
console.log(calc.value) // Access properties: 15
calc.reset() // Call methods
Features
Type-safe: Full TypeScript support with proper type inference
Dual interface: Use as function or access class members
Zero overhead: Minimal runtime cost via Proxy
Clean API: Transparent delegation to call() method
How it works
Creates a Proxy around a bound function
apply trap delegates to instance’s call() method
get trap provides access to instance properties/methods