Is there a way to get keyof working on higher order types in typescript?

Viewed 77

Given a class C, keyof C is a type that consists of members of a class.

However, for an anonymous class that extends C, it seems that the members cannot be retrieved.

For the following minimal reproduction, the type t1 can be determined by typescript, but the type t2 cannot be determined.

Typescript seems to treat the declaration of the constant D as a value and the type information of it seems unusable by typeof.

type Constructor<T> = new (...args: any[]) => T;
class C {
    public a: number = 0;
    public b: number = 1;
}

function hoc(Base: Constructor<C>) {
    return class extends Base {
        public x: string = 'test'
    }
}

const D = hoc(C);
type t1 = keyof C;
type t2 = keyof D;

Typescript Playground reproduction

1 Answers
type t2 = keyof InstanceType<typeof D>;
Related