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();