There is a 3rd party module with the following structure:
export class Container{
static async action() {
return {...}
}
constructor(params = {}) {
// ...
}
async doSomething(params = {}) {
// ...
}
}
I am looking to define this in a `d.ts` file as follows:
declare module 'container' {
interface ContainerImpl<T>{
doSomething(params: Partial<T>): Promise<T>
}
export const Container: {
new <T>(params: T): ContainerImpl<T>;
action<T>(): Promise<T>
}
}
The above definition is close to what I need. I want to make the export generic, so that the TypeScript compiler understands that the static side does not need to explicitly specify `T
` for `action(): Promise<T>
`. What I am aiming for is a Generic Container `Constructible<T>
` where the `new
` method offers `ContainerImpl<T>
`:
import { Container } from 'container';
type MyObject = {
value: string
}
class MyContainer extends Container<MyObject>{}
// Compiler assumes return type as Promise<MyObject>
// since we are extending Conatiner<MyObject>
// This is just an example, ignore the use of top-level await.
await MyContainer.action();
// Expects MyObject type in constructor and assumes a
// `ContainerImpl<MyObject>` is returned from the constructor.
const impl = new MyContainer({value: 'test'});