Class Decorator Solution no longer works

The solution given for the decorators exercise in the typescript course is as follows:

function Sauce(sauce: string) {
 return (constructor: Function) => {
 constructor.prototype.sauce = sauce;
 };
}

however when I ran this code, the resultant objects did not have a .sauce property. I managed to add the property by rewriting this decorator as follows:

function Sauce(sauce: string) {
  return <T extends { new (...args: any[]): {} }>(constructor: T) => {
    return class extends constructor {
      sauce = sauce;
    };
  };
}

@Sauce("Pesto")
class Pizza {}

const pizza = new Pizza();
console.log(pizza);

I can see the sauce property in the output however I am still not able to do console.log(pizza.sauce) as I am getting a ts compile error telling me that the sauce property does not exist on the Pizza class. I am wondering if the syntax given in the solution is deprecated and if so does anyone know how to solve the exercise using up to date syntax in a way the the ts compiler knows about the pizza.sauce property?

2 Likes