I utility type sono tipi generici integrati che trasformano i tipi esistenti, in modo da derivare nuovi tipi anziché ridefinirli. Mantengono i tipi DRY e sincronizzati con la loro fonte.
ts
{ : ; : ; : ; : ; }
<>;
<>;
<>;
<, | >;
<, >;
I utility type sono tipi generici integrati che trasformano i tipi esistenti, in modo da derivare nuovi tipi anziché ridefinirli. Mantengono i tipi DRY e sincronizzati con la loro fonte.
{ : ; : ; : ; : ; }
<>;
<>;
<>;
<, | >;
<, >;
// Update endpoint: accept any subset of fields
function update(id: number, changes: Partial<User>) { ... }
update(1, { name: "New" }); // ✅ only the fields you change
// Create endpoint: everything except the server-generated id
type CreateUser = Omit<User, "id">;
Se aggiungi un campo a User, tutti questi si aggiornano automaticamente — nessuna seconda definizione da dimenticare.
Record<string, number>; // { [key: string]: number } — dictionaries
ReturnType<typeof fn>; // the return type of a function
Parameters<typeof fn>; // tuple of a function's parameter types
NonNullable<string | null>; // string (strips null/undefined)
Extract<T, U>; Exclude<T, U>; // filter members of a union
I utility type ti permettono di esprimere relazioni tra tipi ("il modulo di creazione è l'utente meno l'id") in modo dichiarativo.
Riducono la duplicazione, mantengono i tipi derivati automaticamente coerenti con la loro fonte, e sono ovunque nelle basi di codice reali — in particolare Partial per gli aggiornamenti, Omit/Pick per i DTO, e Record per le mappe.