EMZETT.
Login

Kurz: // Vollständigkeit erzwingen type Tier = “hund” | “katze”; const laute: Record<Tier, string> = { hund: “Wuff”, katze: “Miau” };

Teil des Kurses TypeScript

Kapitel 15 von 15 im Kurs TypeScript (Abschnitt „Nachschlagen“). Mit Fortschritt, Quiz und Zertifikat auf der Lernseite.

Typen im Überblick

SyntaxBedeutung
string, number, boolean, bigint, symbolGrundtypen
null, undefined, void, never, unknown, anySpezialtypen
T[], Array<T>, readonly T[]Arrays
[A, B], [name: A, B?]Tupel
A | BUnion (A oder B)
A & BSchnittmenge (A und B)
"a" | "b", 1 | 2Literaltypen
T | undefined, x?: Toptional
{ a: A; b?: B; readonly c: C }Objekttyp
{ [k: string]: V }, Record<K, V>Index-Signatur
(a: A, b: B) => RFunktionstyp
new (x: A) => TKonstruktortyp
typeof xTyp eines Werts
keyof TSchlüssel-Union
T[K]Indexzugriff
T extends U ? X : Ybedingter 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

TypWirkung
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üfungBeispiel
typeoftypeof x === "string"
instanceofx instanceof Date
in"name" in x
Gleichheitx === null, x.art === "kreis"
Wahrheitif (x)
Array.isArrayArray.isArray(x)
Typprüferfunction ist(x): x is T
Assertionfunction pruefe(x): asserts x is T

Wichtige Compiler-Optionen

OptionEmpfehlung
stricttrue
noUncheckedIndexedAccesstrue
noImplicitOverridetrue
exactOptionalPropertyTypesoptional, sehr streng
targetES2022 oder neuer
module, moduleResolutionESNext + Bundler oder NodeNext
isolatedModulestrue
skipLibChecktrue
noEmittrue, 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

Quiz zur Selbstkontrolle

Weiter im Kurs

Zurück: Gute TypeScript-Praxis

Alle Kapitel: TypeScript im Überblick