Typen im Überblick
| Syntax | Bedeutung |
|---|---|
string, number, boolean, bigint, symbol | Grundtypen |
null, undefined, void, never, unknown, any | Spezialtypen |
T[], Array<T>, readonly T[] | Arrays |
[A, B], [name: A, B?] | Tupel |
A | B | Union (A oder B) |
A & B | Schnittmenge (A und B) |
"a" | "b", 1 | 2 | Literaltypen |
T | undefined, x?: T | optional |
{ a: A; b?: B; readonly c: C } | Objekttyp |
{ [k: string]: V }, Record<K, V> | Index-Signatur |
(a: A, b: B) => R | Funktionstyp |
new (x: A) => T | Konstruktortyp |
typeof x | Typ eines Werts |
keyof T | Schlüssel-Union |
T[K] | Indexzugriff |
T extends U ? X : Y | bedingter Typ |
{ [K in keyof T]: ... } | Mapped Type |
` a${string} ` | Template-Literal-Typ |
Deklarationen
type Alias = string | number;
interface Form { name: string; flaeche(): number }
interface Kreis extends Form { radius: number }
class Hund implements Form { /* ... */ name = ""; flaeche() { return 0; } }
abstract class Basis { abstract m(): void }
enum Farbe { Rot, Gruen }
const obj = { a: 1 } as const;
function f<T extends object>(x: T): T { return x; }
declare const global: string;
export type { Alias };
export default f;Utility-Typen
| Typ | Wirkung |
|---|---|
Partial<T> / Required<T> | alles optional / alles Pflicht |
Readonly<T> | alles schreibgeschützt |
Pick<T, K> / Omit<T, K> | Auswahl / ohne |
Record<K, V> | Wörterbuch |
Exclude<U, X> / Extract<U, X> | Union ohne / nur |
NonNullable<T> | ohne null/undefined |
ReturnType<F> / Parameters<F> | Rückgabe / Parameter |
Awaited<P> | Ergebnis eines Promise |
InstanceType<C> | Instanztyp einer Klasse |
Uppercase<S>, Lowercase<S>, Capitalize<S> | Textumwandlung |
Narrowing-Werkzeuge
| Prüfung | Beispiel |
|---|---|
typeof | typeof x === "string" |
instanceof | x instanceof Date |
in | "name" in x |
| Gleichheit | x === null, x.art === "kreis" |
| Wahrheit | if (x) |
Array.isArray | Array.isArray(x) |
| Typprüfer | function ist(x): x is T |
| Assertion | function pruefe(x): asserts x is T |
Wichtige Compiler-Optionen
| Option | Empfehlung |
|---|---|
strict | true |
noUncheckedIndexedAccess | true |
noImplicitOverride | true |
exactOptionalPropertyTypes | optional, sehr streng |
target | ES2022 oder neuer |
module, moduleResolution | ESNext + Bundler oder NodeNext |
isolatedModules | true |
skipLibCheck | true |
noEmit | true, wenn ein Bundler übersetzt |
Häufige Muster
// Alle Schlüssel eines Objekts durchlaufen, mit Typ
const o = { a: 1, b: 2 };
for (const k of Object.keys(o) as (keyof typeof o)[]) console.log(k, o[k]);
// Vollständigkeit erzwingen
type Tier = "hund" | "katze";
const laute: Record<Tier, string> = { hund: "Wuff", katze: "Miau" };
// Optionaler Zugriff mit Standardwert
const name = (o as { name?: string }).name ?? "Gast";
// Funktion mit Objekt-Parameter und Standardwerten
function setup({ port = 3000, host = "localhost" }: { port?: number; host?: string } = {}) { return `${host}:${port}`; }
console.log(laute.hund, name, setup(), setup({ port: 80 }));Ausgabe
a 1 b 2 Wuff Gast localhost:3000 localhost:80
Weiterlernen
- typescriptlang.org/docs: offizielle Dokumentation und Playground
- Total TypeScript (Matt Pocock) und Type Challenges (GitHub): Übungen
- TypeScript Handbook für Details zu Generics und Typoperatoren
- Nach TypeScript: Framework deiner Wahl (React, Vue, Svelte, Angular) oder Node.js-Backend