https://github.com/arnaugomez/easy-constructor
JavaScript class constructors, without the boilerplate.
https://github.com/arnaugomez/easy-constructor
class constructor javascript object-oriented-programming oop typescript
Last synced: 3 months ago
JSON representation
JavaScript class constructors, without the boilerplate.
- Host: GitHub
- URL: https://github.com/arnaugomez/easy-constructor
- Owner: arnaugomez
- License: mit
- Created: 2024-11-01T17:19:36.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-29T13:57:34.000Z (about 1 year ago)
- Last Synced: 2025-09-23T14:58:30.003Z (6 months ago)
- Topics: class, constructor, javascript, object-oriented-programming, oop, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/easy-constructor
- Size: 1.25 MB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE.md
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
README

Easy Constructor
JavaScript class constructors without the boilerplate
Write JavaScript class constructors in a single line of code.
- Fully type-safe: TypeScript inference that feels like magic :mage:
- Write classes in half the code == half the bugs :bug: == half the bundle size :package:
- No more positional arguments. Enjoy named arguments :love_letter:
Let me show you an example. :point_down:
```ts
// Before
class ExampleClass {
property1: string;
property2: number;
property3: boolean;
property4: string;
// So much boilerplate!
constructor(
property1: string,
property2: number,
property3: boolean,
property4: string,
) {
// Feels like I'm writing the same thing twice.
this.property1 = property1;
this.property2 = property2;
this.property3 = property3;
this.property4 = property4;
}
}
// So many positional arguments. I can't remember what they are!
const exampleInstance = new ExampleClass("Hello", 42, true, "World");
```
```ts
// After
import { easyConstructor } from "easy-constructor";
class ExampleClass {
property1!: string;
property2!: number;
property3!: boolean;
property4!: string;
// Just one line!
static create = easyConstructor(ExampleClass);
}
// Named arguments! π
const exampleInstance = ExampleClass.create({
property1: "Hello",
property2: 42,
property3: true,
property4: "World",
});
```
## Installation
```shell
npm i easy-constructor
```
## Advanced usage
### Optional fields and default values
The `easyConstructor` function treats all class fields as required by default. To make them optional, add them in the `optional` array.
```ts
import { easyConstructor } from "easy-constructor";
class ExampleClass {
property1!: string;
property2!: number;
property3?: boolean;
property4: string = "default";
static create = easyConstructor(ExampleClass, {
optional: ["property3", "property4"],
});
}
const exampleInstance = ExampleClass.create({
property1: "Hello",
property2: 42,
});
```
TypeScript will infer the property names and provide auto-completion.
### Omit variables and custom constructor
You can combine `easyConstructor` with a custom constructor, if you need to initialize some variables with custom logic.
To exclude variables from the easy constructor, use the `omit` array.
```ts
class ExampleClass {
property1!: string;
property2!: number;
property3!: boolean;
property4: number;
static create = easyConstructor(ExampleClass, {
omit: ["property4"],
});
// Custom constructor
constructor(property4: string) {
this.property4 = property4 * 2;
}
}
const exampleInstance = ExampleClass.create(
// Easy constructor arguments
{
property1: "Hello",
property2: 42,
property3: true,
},
// Custom constructor arguments
100,
);
```
### Getters and setters
Easy Constructor works with getters and setters too.
```ts
class ExampleClass {
property1!: string;
get property2() {
return this.property1.length;
}
static create = easyConstructor(ExampleClass, {
// Omit getter and setter properties
// from the easy constructor
omit: ["property2"],
});
}
const exampleInstance = ExampleClass.create({
property1: "Hello",
});
```
### Limitations
Does not support inheritance.
Type inference only works with public fields.
The properties from the easy constructor are assigned after the custom constructor is called. This means that the custom constructor can't access the properties of the easy constructor.
### When should I (not) use Easy Constructor? :thinking:
Easy Constructor is simple, lightweight, and designed to do one thing very well: remove boilerplate from class constructors.
It is designed to replace 99% of the constructors in your app, where you are just assigning arguments to properties. However, if you have a very complex constructor, with hefty initialization logic, you should stick to the traditional constructor or use a factory function.
## Contributors

Arnau GΓ³mez Farell
π» π π π€ π π§ π π§

Josh Goldberg β¨
π§
## Meme gallery
Memes related to easy-constructor. Express the pain of creating class constructors in vanilla JavaScript. Want to contribute a meme? Open a PR!
> π This package was templated with [`create-typescript-app`](https://github.com/JoshuaKGoldberg/create-typescript-app).


