If you already have a subscription, you can sign in.
Enjoy free content straight from your inbox 💌
00:00
The standard way of accessing environment variables within no JS is using process N. Here we have a simple piece of code that logs out the logged in user. However, you can see that we are getting an error on the process variable. If you hover over the error, you can see that it is complaining that cannot find the name process. This is because process is not defined anywhere within our code base and we expect it to be provided by the node JS runtime. TypeScript provides the ability to declare any variables that are not present within the TypeScript code base.
00:34
For example, for the process variable, we can simply declare it as a variable of type any. The syntax for a type declaration is the same as the syntax for declaring any other variable within your code base with two key differences. First, we use declare keyword before declaring the variable, and then we cannot provide any implementation details as they are not really a declaration, but rather a definition. For example, if you try to provide a value for the process variable, TypeScript will complaint that you cannot provide any implementation
01:06
within a declaration. TypeScript supports creating these declarations within a separate file as well. These files are called declaration files. A declaration file is simply a set of declarations and has the file extension D ts. So we can take the type definition for the process variable and create a new file called ND Ts, and you are free to name your declaration files, whatever you want, as long as they end in D Ts. And then we paste this declaration for the process variable.
01:39
Now, in addition to this process variable, there are a lot of other node JS runtime features as well, and we could keep going down this path of creating these declarations ourselves, but fortunately there is a better way. So let's go ahead and delete this declaration file and compile the code using the TypeScript compiler. Now you can see on the compiler output that not only does it tell you that it cannot find this process variable, but also provides a suggestion that you might want to install the type definitions using NPMI types node. Now types is the name of an NPM organization owned
02:13
by Microsoft, and packages under this organization are automatically deployed from a very popular community-driven open source project called Definitely Typed. One of the many type definitions is the one for Node, and we can install it by running NPMI ad types slash node. Once the installation is complete, you can see that the error for the process variable goes away, and the same lack of errors can be observed if you compile a code using the TypeScript compiler. Now, if you go to the definition of the N variable within a code, you can see that it comes from a file called Process dts,
02:48
which is a part of the package that we installed, which is add types slash node. Now, the node JS runtime also provides a number of built-in modules as well, and one of them Is for file system known as fs. Now, in order to use FS or any of the other modules, we simply import it within our code and use it as you normally would within JavaScript. Now, one great thing about these type definitions worth mentioning is that in addition to providing compile time type safety, they also provide great docs for all of the APIs that you are using.
03:20
For example, for this right file sync function, that's it for the node Gs built-ins. Let's talk about third party packages that you might find on NBM. Now, many of those packages are written in TypeScript, so you don't need to do anything special other than simply installing them in order to use them in your code base. There are, however, some packages that require an external type definition, and one such example is express. If we simply install express and try to use it in a simple JavaScript application where we create an express application, set up a route
03:53
and stop listening, you can see that we get a few errors. The key one here is that the express module is not found because it is not described anywhere in TypeScript, similar to what we did for Node, we can actually simply install the type definitions for express that are maintained by the community using NPMI types slash express. Now, once the installation is complete, you can see that the errors within our IDE go away and if we run the compiler on a code base, we don't get any errors on the terminal.