createBuilder()
createBuilder<
State>(initial): BuilderDefinition<State,object>
Creates an immutable builder definition.
Use .step(name, fn) to add steps, then .done() to get the factory.
Each step method on the final builder returns a new builder (immutable).
Type Parameters
State: State
The product type being built
Parameters
initial: State
The initial state
Returns: BuilderDefinition<State, object>
A builder definition to chain .step() calls
Since
2.4.0
Example
const queryBuilder = createBuilder({ table: "", where: [] as string[], limit: 100 })
.step("from", (s, table: string) => ({ ...s, table }))
.step("where", (s, clause: string) => ({ ...s, where: [...s.where, clause] }))
.step("limit", (s, n: number) => ({ ...s, limit: n }))
.done();
const q = queryBuilder()
.from("users")
.where("active = true")
.limit(10)
.build();
BuilderDefinition<State, Methods>
Intermediate builder definition that accumulates steps.
Since
2.4.0
Type Parameters
State: State
The product state being built
Methods: Methods extends object
Accumulated method signatures
Methods
step()
Adds a step to the builder.
Type Parameters
K: K extends string
The method name
Arg: Arg
The argument type for this step
name:
K— The method name
fn:(state, arg) => State— The step function
Returns:BuilderDefinition<State, Methods & { [P in string]: (arg: Arg) => unknown }>
done()
Finalizes the definition and returns a builder factory.
Returns
(): FinalBuilder<
State,Methods>
Returns
FinalBuilder<State, Methods>
FinalBuilder<State, Methods>
FinalBuilder<
State,Methods> ={ [K in keyof Methods]: Methods[K] extends (arg: infer Arg) => unknown ? (arg: Arg) => FinalBuilder<State, Methods> : never }&object
The final builder object with chainable step methods.
Type Declaration
build(): () => State
Returns the built product.
Returns: State
current(): () => State
Returns the current state without finalizing.
Returns: State
Type Parameters
State: State
The product state being built
Methods: Methods extends object
The step method signatures
Since
2.4.0
Director()<B, State>
Director<
B,State> = (builder) =>object
A Director is a function that orchestrates builder steps in a specific sequence. This is the functional equivalent of the GoF Director class.
Type Parameters
B: B
The builder type
State: State
The product type
Parameters
builder: B
Returns: object
build(): () => State
Returns: State
Since
2.4.0
Example
// Director as a simple function
const buildSportsCar: Director<ReturnType<typeof carBuilder>, Car> = (b) =>
b.engine("V8").wheels(4).color("red");
const car = buildSportsCar(carBuilder()).build();