Method decorators
A method decorator is used to annotate a method declaration.
// Method decorator that rounds all number argumentsfunction rounded(target: Object, name: string, descriptor: PropertyDescriptor) { const originalFunction = descriptor.value // Rewrite the property descriptor to a new function that wraps the original descriptor.value = function(...args: number[]) { args = args.map((arg) => Math.round(arg)) // Call the original function return originalFunction.apply(null, args) }}class Adder { // Try commenting the decorator @rounded add(...values: number[]) { return values.reduce((a, b) => (a + b), 0) }}const adder = new Adder()console.log(adder.add(1.1, 2.8, 2.4))