Skip to content

Instantly share code, notes, and snippets.

@hallojoe
Last active January 29, 2022 05:34
Show Gist options
  • Save hallojoe/b5503d88853a88564a87ae227ff1fb6e to your computer and use it in GitHub Desktop.
Save hallojoe/b5503d88853a88564a87ae227ff1fb6e to your computer and use it in GitHub Desktop.
Fluent builder pattern in TypeScript
interface IBuilder<T> {
build(): T
}
interface IValueModel {
name: string
date: Date
}
class FluentValueModelBuilder implements IBuilder<IValueModel> {
public _valueModel: IValueModel = {
date: new Date(),
name: "Undfined"
}
public build(): IValueModel {
return this._valueModel
}
public setName(name: string): this {
this._valueModel.name = name
return this
}
public setDate(date: Date): this {
this._valueModel.date = date
return this
}
}
const result = new FluentValueModelBuilder()
.setDate(new Date())
.setName("Gilfoyle")
.build()
console.log(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment