I am currently converting a JS class to TypeScript. The class extends from the NPM module node-callable-instance (which makes it a subclass of Function internally). Instances of the class can be called like functions. Short example:
import * as CallableInstance from "callable-instance";
class MyClass extends CallableInstance {
constructor() { super('method'); }
method(msg) { console.log(msg); }
}
const inst = new MyClass();
inst('test'); // call will be forwarded to "method()"
It is a requirement of that special project that these instances are callable, other build-time tooling depends on that.
Is there a way to express that in TypeScript? The code above gives
error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'MyClass' has no compatible call signatures.
My first try to workaround this by using a callable interface failed as the class does not implement the call signature...
import * as CallableInstance from "callable-instance";
interface MyClassCallable {
(msg: string): void;
}
class MyClass extends CallableInstance implements MyClassCallable {
constructor() { super('method'); }
method(msg: string): void { console.log(msg); }
}
const inst = new MyClass();
inst('test'); // call will be forwarded to "method()"
The simplest solution is to use interface-class merging and declare an interface with the same name that has the callable signature. The resulting type will have members defined by both the interface and the class:
import * as CallableInstance from "callable-instance";
class MyClass extends CallableInstance {
constructor() { super('method'); }
method(msg: string): void { console.log(msg); }
}
interface MyClass {
(name: string): void
}
const inst = new MyClass();
inst('test'); // call will be forwarded to "method()"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With