https://github.com/jeppech/generic-builder-ts
A generic builder class for TypeScript
https://github.com/jeppech/generic-builder-ts
Last synced: 11 months ago
JSON representation
A generic builder class for TypeScript
- Host: GitHub
- URL: https://github.com/jeppech/generic-builder-ts
- Owner: jeppech
- Created: 2020-11-17T20:32:29.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-01-22T15:58:09.000Z (over 5 years ago)
- Last Synced: 2025-06-26T14:18:29.651Z (12 months ago)
- Language: TypeScript
- Homepage:
- Size: 19.5 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Generic builder for TypeScript
This is small helper class for making buildable objects.
## Installation
Use your favourite package manager.
```
pnpm add @jeppech/generic-builder-ts
```
## Usage
```ts
import { Builder } from '@jeppech/generic-builder-ts'
// Start by declaring an interface, describing your object
interface Person {
name: string
age: number
dob?: Date
}
// Next create a class, with the same name of the interface. This is also known as Declaration Merging.
class Person {
constructer(builder: Builder & Person) {
Object.assign(this, builder)
}
}
// Notice, that you get hints when calling the `with` method.
const person = new Builder(Person)
.with('name', 'John')
.with('age', 25)
.with('dob', new Date('2005-10-13'))
.build()
```
## Acknowledgment
This was inspired by https://github.com/hanterlantant/ts-generic-builder