easy-constructor
Version:
JavaScript class constructors without the boilerplate
28 lines (25 loc) • 1.2 kB
TypeScript
import { ConstructorType } from './types/ConstructorType.js';
import { Class, SetOptional } from 'type-fest';
import { EasyConstructorOptions } from './types/EasyConstructorOptions.js';
/**
* Creates a factory function that initializes a class with its properties.
*
* @example
* ```ts
* class ExampleClass {
* property1: string;
* property2: number;
*
* static create = easyConstructor(ExampleClass);
* }
* // Creates an instance of ExampleClass
* static exampleInstance = ExampleClass.create({ property1: 'value', property2: 42 });
* ```
*
* @param classType The class to instantiate and initialize.
* @param options The configuration of the class, including which fields to omit
* and which fields to make optional.
* @returns A factory function that creates an instance of the class.
*/
declare function easyConstructor<T, Arguments extends unknown[], TOmit extends keyof ConstructorType<T> = never, TOptional extends keyof ConstructorType<T> = never>(classType: Class<T, Arguments>, options?: EasyConstructorOptions<TOmit, TOptional>): (input: SetOptional<Omit<ConstructorType<T>, TOmit>, TOptional>, ...constructorArguments: Arguments) => T;
export { easyConstructor };