From the course: TypeScript Essential Training

Installing TypeScript

- [Instructor] TypeScript is a super set of the JavaScript language, which means that you'll need to compile your TypeScript code into JavaScript in order to run it in a browser or other JavaScript-based environments, such as Node. So before we get into the TypeScript syntax, we first need to install the TypeScript compiler. To begin, head on over to typescriptlang.org/download. Here you'll find a number of ways to bring the TypeScript compiler into your project, depending on what tools you may be using. In this course, however, I'm gong to be relying on the npm environment since that's what I expect the majority of JavaScript applications are using these days or if your project isn't currently using npm, it's easy enough to add it without disturbing anything else. Note that the TypeScript downloads page actually shows two ways to install TypeScript: in your project or at the global level. This means that it's available for your entire machine. I'll get back to this in a minute but first, regardless of where or how you install TypeScript, if you don't already have Node, you'll have to install it by heading on over to nodejs.org. And follow the instructions for installing Node and npm for your particular environment. You'll probably have the best luck with whatever the current LTS version is. I've already got Node and npm installed, so I'm going to skip this step. Once you've made sure Node and npm are installed, it's time to install the TypeScript package. I highly recommend that you install TypeScript at the project level so that each one of your projects may use a different version of TypeScript and you can manage them independently without worrying about one project breaking another. To do that, simply copy this line, head on back to your project, open a terminal in your project's root folder and paste and run this command. This command tells npm to install the TypeScript package into your local project as a dev dependency, meaning that it'll only be needed at development time and not used by your application at runtime. At this point, if I were adding TypeScript to my project, as I usually do, I'd simply hit enter here. However, since I'm going to be jumping around between projects and folders throughout this course, I'm actually going to install TypeScript to my entire machine using the global method I mentioned earlier. To do that, I'll simply remove the save-dev part of this command. And replace with -g to indicate that it should be installed at the global level. Now I'll hit Enter and install the TypeScript compiler globally. Note that like this user, your user may not have the privileges required to install at the global level. Since I'm on a Mac, I'll just rerun this command with the sudo application to gain temporary privileges, like this. You'd use the same command if you're on a Linux machine too. If you're using Windows, you'll need to execute this command in an admin console. After the command completes, I can ensure everything was installed correctly by running the TypeScript compiler executable tsc using the -v flag to display the version. This command worked without any errors, which means I'm ready to go. Now, don't worry if your version of the TypeScript compiler is higher than mine. Even if it's a lot higher. Generally speaking, new versions of TypeScript are pretty backwards compatible so even if your version is higher than the one you see here, everything I show in this course will still work for you.

Contents