import createBuilderGuard, { Builder } from '@sebowy/builder-guard';
interface ICarData {
color: 'red' | 'green' | 'blue';
doors: 3 | 5;
engine: 'V6' | 'V8';
}
interface ICarTypeResult {
carType: 'sport' | 'van';
}
class CarTypeBuilder extends Builder<ICarData, ICarTypeResult> {
build(): ICarTypeResult {
if (this.isSportCar()) {
return { carType: 'sport' };
}
return { carType: 'van' };
}
private isSportCar(): boolean {
return this._data.color === 'red' && this._data.engine === 'V8' && this._data.doors === 3;
}
}
const builder = createBuilderGuard(CarTypeBuilder);
const carTypeResult = builder
.color('red')
.doors(3)
.engine('V8')
// build method is not available until all properties are set
.build();