Ts
Getting Started with TypeScript
  •  

Simple TS Hello World Example

PRO

Let’s jump right into our first example of Typescript code! Since Typescript is an extension of the Javascript language, we will being 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 lets 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!