Created
April 17, 2023 23:38
-
-
Save loicnestler/63fbfdb70a0a9e84d4b3b97828dad1ff to your computer and use it in GitHub Desktop.
A utility function for creating a constructor signature out of a TypeScript type
This file contains hidden or 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
export interface Type<Props, Args> extends Function { | |
new (args: Args): Props | |
} | |
export const ClassFor = <T extends object>(): Type<T, T> => { | |
const Class = function (options: T) { | |
for (const key in options) { | |
Class.prototype[key] = options[key] | |
} | |
} | |
return Class as unknown as Type<T, T> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment