TypeScript Installation


Installing TypeScript on your computer will allow you to write TypeScript files and transcompile them into JavaScript files. Browsers cannot interpret TypeScript files but they can interpret and run JavaScript files. This is why you need to transcompile TypeScript into JavaScript. Node.js can run JavaScript files but it cannot run TypeScript files.

You must first have Node.js installed before you can install TypeScript.

Installation Steps

  1. Open a command prompt (terminal) in any directory
  2. Enter this: npm install -g typescript
  3. Check that it was installed by entering tsc

Compile Your Hello World Program

To test that the TypeScript Compiler (tsc) is working correctly you can write a simple TypeScript hello world program and ask the compiler to compile it into a JavaScript file.

If you haven’t already done so, create a folder to hold all of your TypeScript projects. For example C:\Sites\TypeScript. In that folder create a new folder for your hello world project called helloworld. Open a command prompt and navigate to that folder. If you are using Git Bash as I am you can open Windows Explorer, navigate to the folder, right-click the folder and select Git Bash Here to open a command prompt. Using your favorite text editor, create a file called helloworld.ts as follows:

console.log("Hello world.");

function hello(name : String){
	console.log("Hello " + name);
}

hello("Bob");

In the command prompt compile this to JavaScript with the following command: tsc helloworld.ts. If all went well without errors you will get no message. You will back to the command prompt. Now, to see the output in the console, enter this at the console (command prompt): node helloworld.js

In this code example we have used TypeScript when we defined the name as a String in the hello() function.

Series Navigation<< TypeScript IntroductionTypeScript Types >>

Leave a comment

Your email address will not be published. Required fields are marked *