// 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
});-
Notifications
You must be signed in to change notification settings - Fork 0
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.
twenty9-labs/builder-for
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
ย | ย | |||
ย | ย | |||
ย | ย | |||
ย | ย | |||
Repository files navigation
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.