
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
type-safe-builder-pattern
Advanced tools
Implementation of the 'Builder' design pattern in typescript
A lightweight, zero-dependency TypeScript implementation of the Builder Design Pattern. This package ensures total type safety by preventing you from calling .build() until all required properties of your schema are defined.
.build() method is hidden via TypeScript's type system until every required property is set..set() call returns a new instance of the builder, allowing for easy branching and state sharing without side effects.# Using npm
npm install type-safe-builder-pattern
# Using bun
bun add type-safe-builder-pattern
The core power of this library is that it makes "incomplete" objects impossible to build.
import { objectBuilder } from "type-safe-builder-pattern";
type User = {
id: number;
username: string;
email: string;
isAdmin?: boolean; // Optional
};
const builder = objectBuilder<User>()
.set("id", 1)
.set("username", "romeosarkar");
// ERROR: .build() does not exist yet because 'email' is missing!
// builder.build();
const user = builder.set("email", "romeo@example.com").build(); // Now it works!
console.log(user);
// { id: 1, username: "romeosarkar", email: "romeo@example.com" }
Since each .set() returns a new instance, you can create a base configuration and branch off it.
const baseConfig = objectBuilder<Config>()
.set("host", "localhost")
.set("port", 5432);
const devConfig = baseConfig.set("database", "dev_db").build();
const prodConfig = baseConfig.set("database", "prod_db").build();
The builder maintains full type safety for complex nested structures.
type Project = {
name: string;
tags: string[];
metadata: {
version: string;
};
};
const myProject = objectBuilder<Project>()
.set("name", "TypeSafeApp")
.set("tags", ["typescript", "builder"])
.set("metadata", { version: "1.0.0" })
.build();
This project uses Bun for development and testing.
# Install dependencies
bun install
# Run tests
bun test
# Build the project
bun run build
# Format code
bun run format
MIT © Romeo Sarkar
In large-scale applications, creating complex objects with many required fields can become messy. Using this builder:
.set(key, value) calls are often clearer than large object literals.FAQs
Implementation of the 'Builder' design pattern in typescript
The npm package type-safe-builder-pattern receives a total of 0 weekly downloads. As such, type-safe-builder-pattern popularity was classified as not popular.
We found that type-safe-builder-pattern demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.