Exporting variables

As we have with other exported elements, we are also able to export variables that have been defined within a module. Consider the following export in lib/Module1.ts:

var myVariable = "This is a variable."; 
export { myVariable } 

Here, we are defining a variable named myVariable, and setting the value within the Module1.ts file. We are then exporting the variable itself, by wrapping the variable name in braces, that is, { myVariable }. We can then import and use this variable as follows:

import { myVariable } from './lib/Module1'; 
console.log(myVariable); 

While this may seem a little strange, and at first sight is breaking object-oriented coding principles, this technique is used by numerous frameworks to inject functionality into existing singleton instances. We will explore this technique later in this chapter, when we discuss setting up routes with the Express engine.

Along with variables and functions, interfaces can also be exported using the exports keyword. In large projects, where REST endpoints are used, there are a number of tools that can automatically generate interfaces that describe the inputs and outputs of a REST endpoint. Using these tools will create a large number of interface definitions that can be imported into our code.

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

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