Using decorator metadata

In order to use this extra information within a decorator, we will need to use a third-party library named reflect-metadata. We will discuss how to use third-party libraries in detail in future chapters, but for the time being, this library can be included in our project by typing the following from the command line:

npm install reflect-metadata --save-dev

Once this has been installed, we will need to reference it in our TypeScript file by including the following line at the top of the file:

import 'reflect-metadata'; 

Before we attempt to compile any code that is using the reflect-metadata library, we will need to install the declaration file for this library, as follows:

npm install @types/reflect-metadata --save-dev

We will discuss declaration files in detail in the next chapter.

We can now start to use this class metadata by calling the Reflect.getMetadata function within our decorator. Consider the following update to our earlier parameter decorator:

function metadataParameterDec(target: any,  
    methodName : string,  
    parameterIndex: number) { 
 
   let designType = Reflect.getMetadata( 
       "design:type", target, methodName); 
   console.log(`designType: ${designType}`)     
    
   let designParamTypes = Reflect.getMetadata( 
       "design:paramtypes", target, methodName); 
   console.log(`paramtypes : ${designParamTypes}`); 
    
   let designReturnType = Reflect.getMetadata( 
       "design:returntype", target, methodName); 
   console.log(`returntypes : ${designReturnType}`); 
} 

Here, we have updated our parameter decorator, with three calls to the Reflect.getMetadata function. The first is using the 'design:type' metadata key. This is the same metadata key that we saw earlier in the generated JavaScript, where the compiler generated calls to the __metadata function. We are then logging the result to the console. We then repeat this process for the 'design:paramtypes' and 'design:returntype' metadata keys. The output of this code is as follows:

designType: function Function() { [native code] }
paramtypes : function Number() { [native code] },function String() { [native code] }
returntypes : function Number() { [native code] }

We can see from this output, then, that the nature of the print function (as recorded by the 'design:type' metadata key) is a Function. We can also see that information returned by the 'design:paramtypes' key is an array that includes a Number and a String. This array, therefore, indicates that the function has two parameters, the first of type Number, and the second of type String. Finally, our return type for this function is a Number.

Metadata that is generated automatically by the TypeScript compiler, and that can be read and interrogated at runtime, can be extremely useful. In other languages, such as C#, this type of metadata information is called reflection, and is a fundamental principle when writing frameworks for dependency injection, or for generating code analysis tools.

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

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