As per the documentation of Typescript, the correct type for defining a class constructor is as follows:
type Class = { new (...args: any[]): {} }
If you try to access this type like this:
type ClassPrototype = { new (...args: any[]): {} }["prototype"]
// OR
type ClassPrototype = Class["prototype"]
The compiler will not show any errors in the above cases. However, when attempting the following code, an error occurs:
type PrototypeOf<T extends { new (...args: any[]): {} }> = T["prototype"] // <-- Type '"prototype"' cannot be used to index type 'T'.
The reason behind this behavior is unclear. If indexing Class
with Class["prototype"]
works fine, then why does it fail when using generics like <T extends Class>
and T["prototype"]
? As a temporary workaround, replacing <T extends Class>
with <T extends { prototype }>
seems to resolve the issue but doesn't seem very clean.
To see how this context plays out, check out this playground link.