Chuck's Academy

Decorators and Namespaces in TypeScript

Parameter Decorators

Parameter decorators are used to add metadata to the parameters of a method in a class. These decorators receive three arguments: the prototype of the class, the name of the method, and the index of the parameter in the method's argument list.

Syntax of a Parameter Decorator:

typescript

In this example, the parameterDecorator is applied to the param1 parameter of the exampleMethod method of the ExampleClass.

Practical Example:

Suppose we want to implement a decorator that validates that a parameter is not null or undefined before the method is executed.

typescript

In this example, the validateParameter decorator ensures that the param passed to the exampleMethod is not null or undefined. If null or undefined is passed, it will throw an error before the method is executed.


Support Chuck’s Academy!

Enjoying this course? I put a lot of effort into making programming education free and accessible. If you found this helpful, consider buying me a coffee to support future lessons. Every contribution helps keep this academy running! ☕🚀

Buy Me A Coffee

Chat with Chuck

Loading...
Chat with Chuck AI