Доки по разработке
This project is maintained by teniryte
Принципы
this.addKetchup, addMustard).this из каждого метода делает API цепочечным и читабельным.Пример
class HotDog {
constructor(
public bread: string,
public ketchup?: boolean,
public mustard?: boolean,
public kraut?: boolean,
) {}
addKetchup() {
this.ketchup = true;
return this;
}
addMustard() {
this.mustard = true;
return this;
}
addKraut() {
this.kraut = true;
return this;
}
}
const myLunch = new HotDog('gluten free')
.addKetchup()
.addMustard()
.addKraut();
Практика
this, чтобы сохранить доступ к методам строителя; при наследовании используйте this как тип (this: this).return new HotDog(...this, { ketchup: true })).