Parameter decorators

Parameter decorators are prefixed to parameter declarations, and they are applied to a function for a class constructor or method declaration. This is the signature of ParameterDecorator:

declare type ParameterDecorator = (target: Object, propertyKey: string | symbol, parameterIndex: number) => void; 

Now, let's define the Customer class and use a parameter decorator to decorate a parameter in order to make it required, and validate whether the value has been served:

class Customer { 
constructor() { }
getName(@logging name: string) {
return name;
}
}

Here, the name parameter has been decorated with @logging. The parameter decorator implicitly takes three inputs, namely prototype of the class that has this decorator, name of the method that has this decorator, and index of the parameter that is being decorated. The logging function implementation of the parameter decorator is as illustrated:

function logging(target: any, key : string, index : number) {  
console.log(target);
console.log(key);
console.log(index);
}

Here, target is the class that has the decorator, key is the function name, and index contains the parameter index. This code just logs target, key, and index to the console.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.183.89