Skip to content

Latest commit

 

History

History
69 lines (45 loc) · 1.66 KB

File metadata and controls

69 lines (45 loc) · 1.66 KB

Typescript

TypeScript is a superset of JavaScript which primarily provides optional static typing, classes and interfaces. One of the big benefits is to enable IDEs to provide a richer environment for spotting common errors as you type the code.

Installing

npm install -g typescript

Features

Optional Typing

When there’s a variable or an argument in a function or method call, you can annotate your code with types. To annotate, follow the variable or argument with a colon and followed by it’s type.

var myName: string = "Andrew";

function printName(name: string) {
    console.log(name);
}

Interfaces

Interfaces are great ways to set up agreements on the shape of object literals.

interface Contact {
    name: string,
    email: string,
    phone: string
}

var addressBook: Contact[] = [];

Classes

TypeScript uses ECMAScript 6 classes and adds a little TypeScript goodness.

interface Point {
    x: number,
    y: number
}

class Monster {
    constructor(public name: string, public initialPosition: Point) {

    }
}

var scary = new Monster("Alien", {x: 0, y: 0});

Resources

Examples

VSCode extensions