I have a generic interface like so:
interface A<T extends Object> {
b: T;
}
Currently "b" is stored as an instance of T, however I would like to store the non-instanced version of T as a value within the interface.
My initial thought was to do something like this:
interface A<T extends Object> {
b: typeof T;
}
However I don't think this is correct, nor is it allowed by the compiler. Can this be done?
This below succeeds in what I want to achieve, however it does not enforce 'b' to the generic:
interface A<T extends Object> {
b: Object;
}
CodePudding user response:
You can use a construct signature to refer to the type of a class constructor. It looks like this:
interface A<T extends object> {
instance: T;
ctor: new (...args: any) => T;
}
And here's an example of using it:
const aDate: A<Date> = {
instance: new Date(),
ctor: Date
} // okay