Utility Types
Partial
typescriptinterface User { id: string; name: string; email: string; } function updateUser(id: string, updates: Partial<User>) { // Updates can have any subset of User properties const user = getUser(id); return { ...user, ...updates }; }
Required
typescriptinterface PartialUser { id?: string; name?: string; email?: string; } type CompleteUser = Required<PartialUser>; // All properties are now required
Readonly
typescriptinterface Config { apiUrl: string; timeout: number; } const config: Readonly<Config> = { apiUrl: 'https://api.example.com', timeout: 5000 }; // config.apiUrl = '...'; // ❌ Cannot assign to readonly
Conditional Types
typescripttype NonNullable<T> = T extends null | undefined ? never : T; type Response<T> = T extends string ? { message: T } : { data: T }; // Usage type StringResponse = Response<string>; // { message: string } type NumberResponse = Response<number>; // { data: number }
Mapped Types
typescripttype Getters<T> = { [K in keyof T as `get${Capitalize<K & string}>`]: () => T[K] }; interface User { name: string; age: number; } type UserGetters = Getters<User>; // { // getName: () => string; // getAge: () => number; // }
Conclusion
Advanced TypeScript types enable powerful type-safe abstractions.