I want a class that can dyanmically add getters based on given enum values. The code is like below:
enum TextColor {
black,
red,
green,
blue,
}
enum BgColor {
blackBg,
redBg,
greenBg,
blueBg,
}
enum Style {
bold,
italic,
underline
}
class Painter {
protected textColor: TextColor
protected bgColor: BgColor
protected styles: Set<Style> = new Set()
paint() {
console.log(`paint with ${this.textColor}, ${this.bgColor}, ${this.styles}`)
}
static create(): Painter{
return new Painter()
}
// I don't want o manually add these:
white(color: TextColor) {
this.textColor = color
}
whiteBg(bgColor: BgColor) {
this.bgColor = bgColor
}
bold() {
this.styles.add(Style.bold)
}
}
I also want to prevent same type getters such as TextColor
and BgColor
being called more than once, but for Style
, each value can be called once. What I would like achieve is like below:
Painter.create().white.redBg.bold.underline.paint() // ok
Painter.create().bold.white.blueBg.underline.bold.paint() // error that says second bold does not exist.
Painter.create().redBg.white.underline.bold.underline.paint() // error that says second underline does not exist.
Painter.create().underline.italic.white.redBg.italic.underline.paint() // error that says second italic does not exist.
Is it possible?