Type Alias InstanceTypeAll<T>

InstanceTypeAll<T>: T extends (new (...args: any[]) => infer R)
    ? R
    : T extends {
            prototype: infer P;
        }
        ? P
        : any

Like InstanceType, but also works for private constructors:

Type Parameters

  • T
class ClassPrivateConstructor {
private constructor() {}
aMethod(arg: string): string {return 'foo'}
anotherMethod(num: number): number {return 42}
aField = 'foo'
anotherField = 42
}

// InstanceType fails:
// @ts-expect-error Error: TS2344: Type typeof ClassPrivateConstructor does not satisfy the constraint abstract new (...args: any) => any, Cannot assign a private constructor type to a public constructor type.
type InstanceTypeOfClassPrivateConstructor = InstanceType<typeof ClassPrivateConstructor>

// InstanceTypeAll works! Type is equal to ClassWithPrivateConstructor
type InstanceTypeAllOfClassPrivateConstructor = InstanceTypeAll<typeof ClassPrivateConstructor>
expectType<TypeEqual<InstanceTypeAllOfClassPrivateConstructor, ClassPrivateConstructor>>(true)
expectType<TypeEqual<InstanceTypeAllOfClassPrivateConstructor, {
aMethod: (arg: string) => string,
anotherMethod: (num: number) => number;
aField: string,
anotherField: number
}>>(true)