All notes
Typescrip

Commandline


tsc greeter.ts
// will generate greeter.js.

Features

Type annotations



function greeter(person: string) {
  return "Hello, " + person;
}

let user = [0, 1, 2];
document.body.innerHTML = greeter(user);

Basic types



let isDone: boolean = false;

let decimal: number = 6;
let hex: number = 0xf00d;
let binary: number = 0b1010;
let octal: number = 0o744;

let color: string = "blue";

//----- Template strings

// Surrounded by the backtick/backquote (`), can span multiple lines and have embedded expressions.

let fullName: string = `Bob Bobbington`;
let age: number = 37;
let sentence: string = `Hello, my name is ${ fullName }.
I'll be ${ age + 1 } years old next month.`;

//----- Arrays

let list: number[] = [1, 2, 3];
// The second way uses a generic array type, Array<elemType>:
let list: Array<number> = [1, 2, 3];

//----- Tuple

// Declare a tuple type
let x: [string, number];

// Initialize it
x = ["hello", 10]; // OK
// Initialize it incorrectly
x = [10, "hello"]; // Error

// When accessing an element outside the set of known indices, a "union type" is used instead:
x[3] = "world"; // OK, 'string' can be assigned to 'string | number'
console.log(x[5].toString()); // OK, 'string' and 'number' both have 'toString'
x[6] = true; // Error, 'boolean' isn't 'string | number'

//----- Enum

enum Color {Red = 1, Green, Blue}
let colorName: string = Color[2];

alert(colorName); // Displays 'Green' as it's value is 2 above

//----- Any

let notSure: any = 4;
notSure.ifItExists(); // okay, ifItExists might exist at runtime
notSure.toFixed(); // okay, toFixed exists (but the compiler doesn't check)

let prettySure: Object = 4;
prettySure.toFixed(); // Error: Property 'toFixed' doesn't exist on type 'Object'.

//----- Void

// void is a little like the opposite of any: the absence of having any type at all. You may commonly see this as the return type of functions that do not return a value:

function warnUser(): void {
    alert("This is my warning message");
}

//----- Null, Undefined, --strictNullChecks

// By default null and undefined are subtypes of all other types. That means you can assign null and undefined to something like number.

// However, when using the --strictNullChecks flag, null and undefined are only assignable to void and their respective types.

//----- Never

// Function returning never must have unreachable end point
function error(message: string): never {
    throw new Error(message);
}

Type assertions



let someValue: any = "this is a string";

let strLength: number = (<string>someValue).length;
// And the other is the as-syntax:
let strLength: number = (someValue as string).length;

Interfaces


interface Person {
    firstName: string;
    lastName: string;
}

function greeter(person: Person) {
    return "Hello, " + person.firstName + " " + person.lastName;
}

let user = { firstName: "Jane", lastName: "User" };

document.body.innerHTML = greeter(user);

Classes



class Student {
    fullName: string;
    constructor(public firstName: string, public middleInitial: string, public lastName: string) {
        this.fullName = firstName + " " + middleInitial + " " + lastName;
    }
}

interface Person {
    firstName: string;
    lastName: string;
}

function greeter(person : Person) {
    return "Hello, " + person.firstName + " " + person.lastName;
}

let user = new Student("Jane", "M.", "User");

document.body.innerHTML = greeter(user);