While compilation is incorrect at compile time, for class methods, you can use method decorators to wrap these methods with additional functions at run time. Consider this decorator example, which forces invocation to also write received arguments to the console:
function log(target: Object, key: string, descriptor: any) {
return {
value: function (...args: any[]) {
console.log(args);
return descriptor.value.apply(this, args);
}
};
}
, :
class Calculator {
@log
add(a: number, b: number) {
return a + b;
}
}
: Typescript :
<T>(target: Object, propertyKey: string | symbol, descriptor: PropertyDescriptor<T>) => PropertyDescriptor<T> | void;
, :
, .