Skip to content

Instantly share code, notes, and snippets.

@gund
Last active March 10, 2021 00:13
Show Gist options
  • Save gund/48a62f514769915b6e9fda00e1e23d2b to your computer and use it in GitHub Desktop.
Save gund/48a62f514769915b6e9fda00e1e23d2b to your computer and use it in GitHub Desktop.
New abstract constructor type in Typescript 4.2 with support of the constructor arguments
type AsFunction<T> = T extends (...args: any) => any ? T : never;
type InferConstructor<T> = T extends { constructor: infer C }
? AsFunction<C>
: () => any;
type Constructor<T> = new (...args: Parameters<InferConstructor<T>>) => T;
type AbstractConstructor<T> = abstract new (
...args: Parameters<InferConstructor<T>>
) => T;
interface Test {
constructor(prop: string);
method1(): void;
};
type TestType = Constructor<Test>;
const Test: TestType = null;
const test = new Test('smth');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment