Hello World in TypeScript

Our first program will print the classic “hello world” message. Here’s the full source code.

// hello-world.ts
console.log("hello world");

To run the program, you will need to compile the TypeScript code into JavaScript and then execute it using Node.js.

First, install TypeScript if you don’t have it:

$ npm install -g typescript

Then, compile the TypeScript code into JavaScript:

$ tsc hello-world.ts

Now, you can run the resulting JavaScript file:

$ node hello-world.js
hello world

Sometimes we’ll want to build our programs into a single file. TypeScript compilation achieves this by producing an equivalent .js file.

First, compile the TypeScript file:

$ tsc hello-world.ts
$ ls
hello-world.js   hello-world.ts

We can then execute the compiled JavaScript file directly using Node.js.

$ node hello-world.js
hello world

Now that we can run and build basic TypeScript programs, let’s learn more about the language.