如何在TypeScript严格模式下键入此对象生成器而不会使其膨胀?

时间:2019-06-12 15:53:26

标签: typescript builder

我喜欢我在网上找到的这个构建器模式,但是在严格模式下,它将不起作用,因为我获得的前三个属性与第一个属性具有相同的错误:

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

我想避免给numberOfSlicesisThindoughType提供默认值,因为这似乎打败了构建者的想法。我无法将它们设置为undefined,因为那样行不通。

是否有避免过度膨胀的解决方案?添加布尔值以检测是否已设置某些内容似乎是一场噩梦。

1 个答案:

答案 0 :(得分:1)

TypeScript抱怨是因为在严格模式下,undefined无法分配给类型numberbooleanDoughType,并且在严格模式下,每个类属性必须为用其类型的值初始化。

由于这些属性可能未定义,因此可以使用包含undefined作为有效值的union type显式键入它们:

private numberOfSlices: number | undefined;
private isThin: boolean | undefined;
private doughType: DoughType | undefined;
private toppings: Topping[] = [];

关于严格类初始化here in the TypeScript 2.7 relase notes的更多信息。