Answers for "simple typescript decorator example"

0

simple typescript decorator example

// Example of a Class Decorator in TypeScript:
function reportableClassDecorator<T extends { new (...args: any[]): {} }>(constructor: T) {
  return class extends constructor {
    reportingURL = "http://www...";
  };
}
 
@reportableClassDecorator
class BugReport {
  title: string;
  constructor(t: string) {
    this.title = t;
  }
}
 
const bug = new BugReport("Needs dark mode");
console.log(bug.title); // Prints "Needs dark mode"
console.log(bug.reportingURL); // Prints "http://www..."
Posted by: Guest on February-17-2022

Code answers related to "TypeScript"

Browse Popular Code Answers by Language