TypeScript enables developers to use the latest features of JavaScript by transpiling .ts files into older .js versions that browsers or Node.js can understand.

It also makes programming large applications easier to manage because it enforces static typing (variables declared with a type).

Installing TypeScript

Install globally using npm:

$ npm install -g typescript

Now that the compiler is installed use it to convert .ts into .js:

// main.ts
let firstName: string = "John";

# creates main.js
$ tsc main.ts
$ node main.js

We used let an ECMAScript 2015 feature and it transpiled into var if you take a look at main.js:

// main.js
var firstName = "John";


TypeScript enforces type safety either by declaring the type firstName: string or if you omit the type it is assigned by TypeScript.

Try changing it to a number and you’ll get a compile time error:

// main.ts
let firstName: string = "John";
firstName = 2;
main.ts(3,1): error TS2322: Type 'number' is not assignable to type 'string'.

Make the variable of type any to be allowed to change type:

// main.ts
let firstName: any = "John";
firstName = 2;


Variables should not be defined globally to prevent name clashes with other libraries. Classes encapsulate the variables to prevent this.

TypeScript adds to JavaScript ES6 classes with public, private, and proteced variables.

// person.ts
export class Person {
    public firstName: string = "John";
// main.ts
import {Person} from "./person"

let john: Person = new Person();

// compile main.ts using tsc
// tsc main.ts


There’s so much more to TypeScript and for a full in depth tutorial go here.