Ts
Getting Started with TypeScript
  •  

Simple TS Hello World Example

PRO
Outline

Let’s jump right into our first example of Typescript code! Since Typescript is an extension of the Javascript language, we will begin our example by writing valid Javascript code for the Typescript compiler.

// sample.js

function add(x, y) {
    return x + y;
}

console.log(add(2, 2)); // 4

The code above will output successfully, but let's convert this to Typescript syntax by adding types to our function.

// sample.ts

function add(x: number, y: number): number {
    return x + y;
}

console.log(add(2,2)); // 4

Now the function will perform a bit better. If we were to call add() with inputs other than numbers, we would get an error saying that the wrong variable type was provided!

 

I finished! On to the next chapter