Decorators - TypeScript - JavaScript's Superset

Rumman Ansari   Software Engineer   0000-00-00 00:00:00   57  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

Table of Content:

ECMAScript2016 proposes use of Decorators that are currently available as an experimental feature.

Decorator enables us with feature of adding annotations. It can be activated by enabling "experimentalDecorators" compiler. Constructor, properties, parameters and methods can be decorated in TypeScript.

Following is an example of class decorator:

@log
class Physics {
cube: string;
constructor(message: string) {
this.cube = message;
}
helloPhysics() {
return "Hello! " + this.cube;
}
}
Decorators

In the previous code, changing the instance where @log was used just before the method "helloPhysics()" will result to example of method decorator.

Similarly, parameter delcaration can also have parameter decorator.

For instance, see the below code snippet:

@validate
helloPhysics(@required cube: string) {
}
TypeScript Course Summary

In this course, you have learnt about

  • What is Typescript and how it is different from Javascript
  • Type checking and Type annotations
  • Data type classications including built-in and user-defined types
  • Object oriented way of programming with Typescript
  • Modules and Decorators