Skip to content

Instantly share code, notes, and snippets.

@zakhenry
Created September 11, 2018 09:42
Show Gist options
  • Save zakhenry/ae561ad3d0a00723419d7296c97f28cc to your computer and use it in GitHub Desktop.
Save zakhenry/ae561ad3d0a00723419d7296c97f28cc to your computer and use it in GitHub Desktop.
Typescript member property reflection
class Foo {
baz: number;
}
export interface StaticClass<InstanceType = any> {
new (...args: any[]): InstanceType;
}
type Instance<T> = T extends StaticClass<infer U> ? U : never;
function describe<T extends StaticClass>(staticClass: T): {[P in keyof Instance<T>]: P} {
// can't implement this as classes don't have members at runtime! Need to decorate each property to be able to reflect them.
return {} as any;
}
const description = describe(Foo);
const a = description.baz;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment