Skip to content

Instantly share code, notes, and snippets.

@loicnestler
Created April 17, 2023 23:38
Show Gist options
  • Save loicnestler/63fbfdb70a0a9e84d4b3b97828dad1ff to your computer and use it in GitHub Desktop.
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
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