An open API service indexing awesome lists of open source software.

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

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