Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR TYPESCRIPT

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..."
Source by www.typescriptlang.org #
 
PREVIOUS NEXT
Tagged: #simple #typescript #decorator
ADD COMMENT
Topic
Name
1+9 =