TypeScript – Schnellreferenz

TypeScript ist JavaScript mit statischen Typen – der Standard für große Web-Projekte und Angular/React-Anwendungen.

Typen und Interfaces

const name: string = "Hanse";
const alter: number = 42;
const aktiv: boolean = true;

interface Kunde {
    name: string;
    alter: number;
    email?: string;   // optional
}

const k: Kunde = { name: "Hanse", alter: 42 };

Union Types und Type Alias

type Id = string | number;
type Status = "aktiv" | "inaktiv";

let id: Id = 42;
let status: Status = "aktiv";

Generics

function erste<T>(liste: T[]): T | undefined {
    return liste[0];
}

const x = erste([1, 2, 3]);  // number

Klassen

class Kunde {
    constructor(public name: string, private alter: number) {}

    istVolljaehrig(): boolean {
        return this.alter >= 18;
    }
}

tsc-Befehle

AufgabeBefehl
Kompilierennpx tsc datei.ts
Watch-Modusnpx tsc --watch
Konfigurationnpx tsc --init
Strikte Prüfungnpx tsc --strict

Verwandt: JavaScript · HTML & CSS · API