My first TypeScript file

Published:

Now that you have Node.js and TypeScript installed, we can actually get started with TypeScript.

Creating a function with some TypeScript sprinkled on top.

File open in visual studio code

Let’s create our first TypeScript file main.ts with the following contents:

function output(words: string) {
    console.log(words);
}

output("Hello World");

This should look really familiar and comfortable to JS developers. Really, the only TypeScript specific markup in this file is just the method argument type defined as string;

Compiling TypeScript into JavaScript with tsc

Let’s convert it into JavaScript so it can be run in the browser (or with Node.js).

~/my-first-typescript-file ❯❯❯ ls
main.ts
~/my-first-typescript-file ❯❯❯ tsc main.ts
~/my-first-typescript-file ❯❯❯ ls
main.js main.ts

The tsc command is taking the content from the main.ts file and compiling it into a main.js file.

File open in visual studio code

JavaScript Output:

function output(words) { // notice no more `string` type
    console.log(words);
}
output("Hello World");

// output: "Hello World"

The main.js file looks very similar to it’s TypeScript equivalent. The difference is the removal of the arugument type : string in output(words: string).