TypeScript
TypeScript Utility Types
Built-in TypeScript utility types with examples. Transform types without writing custom generics.
Object Type Transformations
| Command / Property | Description |
|---|---|
| Partial<T> | Make all properties optional. Example: Partial<{name: string; age: number}> = {name?: string; age?: number} |
| Required<T> | Make all properties required. Example: Required<{name?: string}> = {name: string} |
| Readonly<T> | Make all properties readonly. Prevents reassignment after creation. |
| Pick<T, K> | Select specific properties. Example: Pick<User, "id" | "name"> = {id: number; name: string} |
| Omit<T, K> | Remove specific properties. Example: Omit<User, "password"> removes password field. |
| Record<K, V> | Create object type with keys K and values V. Example: Record<string, number> = {[key: string]: number} |
Union Type Helpers
| Command / Property | Description |
|---|---|
| Exclude<T, U> | Remove types from union. Example: Exclude<"a" | "b" | "c", "a"> = "b" | "c" |
| Extract<T, U> | Keep only matching types. Example: Extract<"a" | "b" | "c", "a" | "f"> = "a" |
| NonNullable<T> | Remove null and undefined. Example: NonNullable<string | null | undefined> = string |
Function Type Helpers
| Command / Property | Description |
|---|---|
| ReturnType<T> | Extract return type. Example: ReturnType<() => string> = string |
| Parameters<T> | Extract parameter types as tuple. Example: Parameters<(a: string, b: number) => void> = [string, number] |
| ConstructorParameters<T> | Extract constructor parameter types as tuple. |
| InstanceType<T> | Extract instance type of a class constructor. |
String Type Helpers
| Command / Property | Description |
|---|---|
| Uppercase<T> | Convert string literal to uppercase. Example: Uppercase<"hello"> = "HELLO" |
| Lowercase<T> | Convert string literal to lowercase. Example: Lowercase<"HELLO"> = "hello" |
| Capitalize<T> | Capitalize first character. Example: Capitalize<"hello"> = "Hello" |
| Uncapitalize<T> | Lowercase first character. Example: Uncapitalize<"Hello"> = "hello" |
Promise & Awaited
| Command / Property | Description |
|---|---|
| Awaited<T> | Unwrap Promise type. Example: Awaited<Promise<string>> = string |
| Awaited<Promise<Promise<T>>> | Recursively unwraps nested promises to the resolved type. |
Advanced Patterns
| Command / Property | Description |
|---|---|
| Partial<Pick<T, K>> & Omit<T, K> | Make only specific fields optional while keeping the rest required. |
| Record<string, never> | Empty object type -- no properties allowed. |
| Extract<T, Function> | Filter union to only function types. |
| Readonly<Record<string, T>> | Immutable dictionary type. |