create interface class and function in typescript d.ts Summary when you create an library which is can be function or class like below sample import Client from ./client;// The following two lines both achieve the same result.const clas = new Client;const fun = Client; The client may be co
Summary
when you create an library which is can be function or class like below sample
import Client from "./client";
// The following two lines both achieve the same result.
const clas = new Client();
const fun = Client();
The client
may be constructed via the new keyword or implicitly without as a function call.
How you can express this in a types.d.ts file?
Conclusion
The conventional way something like this is done in TypeScript's own standard library (see the typings for Date
for example) is to declare two interface
s; one for the instance type (with the intended class
name) and another for the constructor type (usually the same name with Constructor
appended to it).
And then you declare the class constructor value as a var
or const
.
If you want to give the constructor added functionality, such as being callable without new
, you can do it inside the XXXConstructor
interface.
Like so: (Making a class and function interchangeable in Typescript types.d.ts library definition)
// this you class import, were assumed like this
interface Client {
foo(): void;
}
// create new client constructor
interface ClientConstructor {
new(): Client;
(): Client;
}
// export them
declare const Client: ClientConstructor;
export default Client;