Running our first TypeScript program

Now, let’s compile our first TypeScript program! Create a file called hello.ts and enter the following content:

// dunebook/hello-world/hello-world.ts
console.log('Hello world!');

Since you’ve already installed the TypeScript compiler, you should have a global executable command called tsc. You can use it in order to compile the file:

$ tsc hello.ts

Now, you should see the file hello.js in the same directory where hello.ts is. hello.js is the output of the TypeScript compiler; it contains the JavaScript equivalent to the TypeScript you wrote. You can run this file using the following command:

$ node hello.js

Now, you’ll see the string Hello world! printed on the screen. In order to combine the process of compiling and running the program, you can use the package ts-node:

$ npm install -t ts-node

Now you can run:

$ ts-node hello.ts

You should see the same result, but without the ts-node file stored on the disk.

Chapter 2 of 4