Utility Types
Built-in Power
Partial<T>
Macht alle Properties optional. Perfekt für Update-Funktionen.
interface User {
id: string;
name: string;
email: string;
}
// Nur die Felder die sich ändern
function updateUser(id: string, updates: Partial<User>) {
// ...
}
updateUser('123', { name: 'Alex' }); // ✅ Valid
Pick<T, K> & Omit<T, K>
Erstellt Types mit nur bestimmten Properties (Pick) oder ohne bestimmte (Omit).
// Nur id und name
type UserPreview = Pick<User, 'id' | 'name'>;
// Alles außer id (für Create)
type CreateUserInput = Omit<User, 'id'>;
// Kombiniert: API Response
type UserListResponse = ApiResponse<Pick<User, 'id' | 'name'>[]>;
Record<K, T>
Dictionary-ähnliche Strukturen mit definierten Key- und Value-Types.
// String Keys → User Arrays
type UsersByRole = Record<string, User[]>;
// Enum Keys → spezifische Values
type StatusColors = Record<Status, string>;
const colors: StatusColors = {
pending: '#f59e0b',
active: '#10b981',
error: '#ef4444',
};
Required<T> & Readonly<T>
interface Config {
host?: string;
port?: number;
}
// Alle Properties required
const defaults: Required<Config> = {
host: 'localhost',
port: 3000,
};
// Alle Properties readonly (immutable)
const frozen: Readonly<Config> = { host: 'prod' };
frozen.host = 'dev'; // ❌ Error!