Skip to content

Instantly share code, notes, and snippets.

@ermik
Created April 6, 2021 13:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ermik/11c4a0195e1663ea5b486dc0336cc11d to your computer and use it in GitHub Desktop.
Save ermik/11c4a0195e1663ea5b486dc0336cc11d to your computer and use it in GitHub Desktop.
TS Ideas
type CtorFunc<T extends new (...args: any[]) => any> = T extends new (...args: infer A) => infer R
? (...args: A) => R
: never;
type CtorArgs<T extends new (...args: any[]) => any> = T extends new (...args: infer A) => infer R
? A
: never;
type Ctor<T extends new (...args: any[]) => any> = T extends new (...args: infer A) => infer R
? { new (...args: A): R }
: never;
function createWrapped<T extends new (...args: any[]) => any>(
Cls: T,
): (...args: CtorArgs<T>) => { new (): T } {
return function (...args) {
return class extends Cls {
constructor(..._: any[]) {
super(...args);
}
};
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment