Is there a way to do code weaving in TypeScript?
What I'm trying to do is inject a piece of code as the very first line of every function in my TypeScript app and I don't wont to do this manually (this manual approach is tedious and error prone).
Is there a way to do code weaving in TypeScript?
What I'm trying to do is inject a piece of code as the very first line of every function in my TypeScript app and I don't wont to do this manually (this manual approach is tedious and error prone).
While not true compile-time weaving, for class methods only you can use method decorators to wrap those methods with additional functionality at runtime. Consider this example method decorator, which makes invocation also log the received arguments into the console:
Applying this decorator to a method is as trivial as:
Quick explanation: a method decorator in Typescript has the following signature:
In other terms, the method decorator takes in 3 arguments:
A method decorator returns a single property descriptor, which is a replacement for that of the original method on the type.