Skip to content

The builderFor function creates an object builder with default properties, allowing for deep merging with optional overrides. It uses deepMerge to handle nested objects, enabling flexible object construction with customizable configurations.

Notifications You must be signed in to change notification settings

twenty9-labs/builder-for

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

1 Commit
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Builder for

// Example usage
type Post = {
  id: number;
  title: string;
  author: {
    name: string;
    email: string;
  };
  tags: string[];
  published: boolean;
}

// Create a base post
const basePost: Post = {
  id: 0,
  title: "",
  author: {
    name: "",
    email: "",
  },
  tags: [],
  published: false,
};

// Create a post builder
const postBuilder = builderFor(basePost);

// โœ… Good examples
const post1 = postBuilder({
  title: "Hello World",
  author: { name: "John" }, // Partial nested objects work
});

const post2 = postBuilder({
  title: "TypeScript Tips",
  author: {
    name: "Jane",
    email: "[email protected]",
  },
  published: true,
});

// ๐Ÿ›‘ Bad examples - These will cause type errors
const badPost1 = postBuilder({
  author: {
    name: 123, // Error: number is not assignable to string
  },
});

const badPost2 = postBuilder({
  unknownField: "test", // Error: unknownField does not exist in Post
});

About

The builderFor function creates an object builder with default properties, allowing for deep merging with optional overrides. It uses deepMerge to handle nested objects, enabling flexible object construction with customizable configurations.

Resources

Stars

Watchers

Forks