dvs. strongly typed kode som kompileres til javascript
så slipper vi å teste i runtime om koden parser. vi får errors i console når vi kompilerer, eller allerede i IDE
dessuten får vi mye bedre autocomplete!
akkurat som C# og java!
Noen nye konsepter:
enum Ternary {
FALSE,
TRUE,
MAYBE
}
interface Dog {
breed: string;
weight: number;
}
const a: number = 1;
const b: number = 1.5;
const c: string = 'hello';
const d: string = "world";
const e: string = `${c} ${d}`;
const f: boolean = true;
const g: boolean = false;
Grunnleggende
const myArray: number[] = [1, 2, 3];
const myTuple: [number, number] = [800, 600];
Array og Tuples
type Person = {
name: string,
age: number
};
Objekter
type Animal = {
legs: number
}
type Mammal = {
carnivore: boolean
}
type Feline = {
maxWeight: number
}
type Ocelot = Animal & Mammal & Feline;
const babou: Ocelot = {
legs: 4,
carnivore: true,
maxWeight: 30,
};
const foo: any = 5;
foo.toUpperCase();
foo.this.will.fail;
function meh(bar) {
bar.also.fails();
}
function throwError(message: string): never {
throw new Error(message);
};
function identity<T>(t: T): T {
return t;
}
type Nullable<T> = { [P in keyof T]: T[P] | null }