Last active
March 10, 2021 00:13
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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