After coming across a builder pattern online that I really liked, I found that it doesn't work in strict mode due to receiving the same error for the first 3 properties:
(property) PizzaBuilder.numberOfSlices: number
Property 'numberOfSlices' has no initializer and is not definitely assigned in the constructor.ts(2564)
export enum DoughType {
HEALTHY,
}
export enum Topping {
CHEESE,
}
export interface Pizza {
numberOfSlices: number;
isThin: boolean;
doughType: DoughType;
toppings: Topping[];
}
export class PizzaBuilder {
private numberOfSlices: number;
private isThin: boolean;
private doughType: DoughType;
private toppings: Topping[] = [];
public setNumberOfSlices(numberOfSlices: number): PizzaBuilder {
this.numberOfSlices = numberOfSlices;
return this;
}
public setIsThin(isThin: boolean): PizzaBuilder {
this.isThin = isThin;
return this;
}
public setDoughType(doughType: DoughType): PizzaBuilder {
this.doughType = doughType;
return this;
}
public addTopping(topping: Topping): PizzaBuilder {
this.toppings.push(topping);
return this;
}
public build(): Pizza {
if (this.isThin === undefined) this.isThin = false;
if (this.numberOfSlices === undefined) this.numberOfSlices = 8;
if (this.doughType === undefined) throw new Error('Dough type must be set');
if (this.toppings.length < 1) this.toppings.push(Topping.CHEESE);
return {
numberOfSlices: this.numberOfSlices,
isThin: this.isThin,
toppings: this.toppings,
doughType: this.doughType,
};
}
}
const pizza = new PizzaBuilder()
.setIsThin(true)
.setNumberOfSlices(6)
.setDoughType(DoughType.HEALTHY)
.addTopping(Topping.CHEESE)
.build();
While I want to stay away from assigning default values to numberOfSlices
, isThin
, and doughType
as it goes against the builder concept, setting them to undefined
isn't a viable solution either.
Does anyone know of a solution that doesn't involve unnecessary complexity? Introducing extra booleans to track whether a property has been set seems like it would just add more clutter.