This package is in an early development stage!
Currently there is no stable builder available.
The current phase tests different approaches as well as the deployment itself.
The builder pattern for TypeScript
Version 0.2.0
This library allows you to use the Builder Pattern for types and interfaces in TypeScript. It aims to work dynamically without the need for separately generated builder classes. It should be applicable to existing code without requiring additional modifications.
To generate a builder for the interface "Frog" just create a TSBuilder:
import TsBuilder from "@openfoxes/ts-builder";
interface Frog {
name: string;
color: "green" | "red";
calm: boolean;
}
// Use the TsBuilder:
const immutableFrogBuilder = TsBuilderImmutable.makeBuilderFor<Frog>();
const frog = immutableFrogBuilder
.set("name", "Klaus")
.set("color", "green")
.set("calm", true)
.build();
/**
* In the future the current usages should be possible:
*/
// The js-classic-builder, currently NOT possible:
const classicFrogBuilder = TsBuilder.makeBuilderFor<Frog>();
classicFrogBuilder.set("name", "Klaus").set("color", "green").set("calm", true);
const frog = classicFrogBuilder.build();
// The optimal TsBuilder (using Proxy), currently NOT possible:
const frogBuilder = TsBuilder.makeBuilderFor<Frog>();
const frog = frogBuilder
.set("name", "Klaus")
.set("color", "green")
.set("calm", true)
.build();
Install the package:
npm install @openfoxes/ts-builder
or
npm install -E @openfoxes/[email protected]
This repository strives to make the best possible use of TypeScript's capabilities. However, it is unfortunately not easily possible to provide a fully-fledged builder dynamically and in real-time. Work is ongoing to offer more user-friendly methods in the future and to improve error handling. For now, though, it is already an improvement over relying solely on constructors or manually creating builders.
This project aims to provide a clean and secure design. In order to optimize the underlying code and reducing the risk of bugs, quality tools are used: