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
| Aufgabe | Befehl |
|---|---|
| Kompilieren | npx tsc datei.ts |
| Watch-Modus | npx tsc --watch |
| Konfiguration | npx tsc --init |
| Strikte Prüfung | npx tsc --strict |
Verwandt: JavaScript · HTML & CSS · API