Decorators is a feature of TypeScript that is becoming more and more common in many major libraries. This lesson walks-through what decorators is and how to create your own.
Nomarl to decorate a object:
Const PERSON = {name: ' John '}; function Addage (age) { returnfunction(person) { return { , name:person.name } = addage (person); Console.log (John);//{name: "John", age:30}
In Typescript, we can enable the "Experimentadecorators", which is ES7 feature:
{ "CompilerOptions": { "RootDir": "src", "Module": "Commonjs", "Target": "ES5", "Noimplicitany":false, "Sourcemap":false, "OutDir": "./dist", "Noemitonerror":true, "Experimentaldecorators": True }, "Exclude": [ "Node_modules", "Typings/main", "Typings/main.d.ts" ]}
function Addage (age) { return function (Targetclass) { return class{age = = new Targetclass (). Name; }}} @addAge ( 30 = "Johnn" ;} Const John = new person (); Console.log ( John); // {name: "Johnn", age:30}
As before we create addage function as decorator, different from before, it return class, because we want to decorate Pers On class.
After the decorator, we'll have an age of prop on the person class.
[TypeScript] Understanding Decorators