Advanced Type Tools
Utility Types
Section titled “Utility Types”These are built-in helpers for common type transformations.
interface User { id: number; name: string; email?: string;}
type UserPatch = Partial<User>;type FullUser = Required<User>;type UserPreview = Pick<User, "id" | "name">;type UserWithoutEmail = Omit<User, "email">;type UserMap = Record<string, User>;Mapped Types
Section titled “Mapped Types”Mapped types build new types by iterating over keys.
type ReadonlyUser<T> = { readonly [K in keyof T]: T[K];};
type ImmutableUser = ReadonlyUser<User>;Conditional Types
Section titled “Conditional Types”Conditional types act like type-level if/else.
type IsString<T> = T extends string ? true : false;
type A = IsString<"hello">; // truetype B = IsString<123>; // falsekeyof and typeof
Section titled “keyof and typeof”const config = { apiUrl: "https://example.com", timeout: 5000,};
type Config = typeof config;type ConfigKeys = keyof Config; // "apiUrl" | "timeout"typeof(in type position) creates a type from a value.keyofgets a union of property names.
infer Keyword
Section titled “infer Keyword”infer lets TypeScript extract a type inside a conditional type.
type ReturnTypeOf<T> = T extends (...args: never[]) => infer R ? R : never;
type Fn = (x: number) => string;type Result = ReturnTypeOf<Fn>; // stringVisual Map of Advanced Type Tools
Section titled “Visual Map of Advanced Type Tools”
graph TD
A[Source Type] --> B[Utility Type]
A --> C[Mapped Type]
A --> D[Conditional Type]
D --> E[infer Extract]
A --> F[keyof and typeof]