TypeScript
TypeScript Utility Types Cheatsheet
Quick reference for TypeScript built-in utility types including Partial, Required, Pick, Omit, Record, Exclude, Extract, and ReturnType.
Object Type Utilities
Partial<T>
Makes all properties optional.
interface User { name: string; age: number; }
type MaybeUser = Partial<User>;
// { name?: string; age?: number; }
Required<T>
Makes all properties required.
interface Config { debug?: boolean; verbose?: boolean; }
type StrictConfig = Required<Config>;
// { debug: boolean; verbose: boolean; }
Readonly<T>
Makes all properties readonly.
type FrozenUser = Readonly<User>;
// { readonly name: string; readonly age: number; }
Pick<T, K>
Select a subset of properties.
type NameOnly = Pick<User, "name">;
// { name: string; }
type NameAge = Pick<User, "name" | "age">;
Omit<T, K>
Remove properties.
type WithoutAge = Omit<User, "age">;
// { name: string; }
Record<K, V>
Construct an object type with keys K and values V.
type Roles = Record<string, boolean>;
// { [key: string]: boolean; }
type StatusMap = Record<"active" | "inactive", User[]>;
// { active: User[]; inactive: User[]; }
Union Type Utilities
Exclude<T, U>
Remove types from a union.
type T = Exclude<"a" | "b" | "c", "a">;
// "b" | "c"
Extract<T, U>
Keep only types assignable to U.
type T = Extract<"a" | "b" | "c", "a" | "c">;
// "a" | "c"
type T = Extract<string | number | (() => void), Function>;
// () => void
NonNullable<T>
Remove null and undefined.
type T = NonNullable<string | null | undefined>;
// string
Function Type Utilities
ReturnType<T>
Extract the return type of a function.
function getUser() { return { name: "Alice", age: 30 }; }
type User = ReturnType<typeof getUser>;
// { name: string; age: number; }
Parameters<T>
Extract parameter types as a tuple.
function greet(name: string, age: number): void {}
type Params = Parameters<typeof greet>;
// [name: string, age: number]
ConstructorParameters<T>
Extract constructor parameter types.
type DateParams = ConstructorParameters<typeof Date>;
InstanceType<T>
Extract the instance type of a constructor.
type D = InstanceType<typeof Date>;
// Date
String Utilities
| Type | Description | Example |
|---|---|---|
Uppercase<S> | Uppercase string literal | Uppercase<"hello"> = "HELLO" |
Lowercase<S> | Lowercase string literal | Lowercase<"HELLO"> = "hello" |
Capitalize<S> | Capitalize first letter | Capitalize<"hello"> = "Hello" |
Uncapitalize<S> | Lowercase first letter | Uncapitalize<"Hello"> = "hello" |
Promise Utilities
Awaited<T>
Unwrap the resolved type of a Promise.
type T = Awaited<Promise<string>>; // string
type T = Awaited<Promise<Promise<number>>>; // number
Mapped Type Modifiers
// Remove optional
type Concrete<T> = { [K in keyof T]-?: T[K] };
// Remove readonly
type Mutable<T> = { -readonly [K in keyof T]: T[K] };
// Remap keys
type Getters<T> = { [K in keyof T as `get${Capitalize<string & K>}`]: () => T[K] };
Conditional Types
type IsString<T> = T extends string ? true : false;
type A = IsString<"hello">; // true
type B = IsString<42>; // false
Inferring Types
type ElementOf<T> = T extends (infer E)[] ? E : never;
type T = ElementOf<string[]>; // string
type UnpackPromise<T> = T extends Promise<infer U> ? U : T;
type T = UnpackPromise<Promise<number>>; // number
Template Literal Types
type EventName = `on${Capitalize<"click" | "focus">}`;
// "onClick" | "onFocus"
type CSSProperty = `${string}-${string}`;
Quick Reference Table
| Utility | Input | Output |
|---|---|---|
Partial<{ a: string }> | Required props | { a?: string } |
Required<{ a?: string }> | Optional props | { a: string } |
Readonly<{ a: string }> | Mutable props | { readonly a: string } |
Pick<{ a; b }, "a"> | Object + keys | { a } |
Omit<{ a; b }, "a"> | Object + keys | { b } |
Record<"a", number> | Keys + value type | { a: number } |
Exclude<"a"|"b", "a"> | Union - removed | "b" |
Extract<"a"|"b", "a"> | Union - kept | "a" |
NonNullable<T|null> | Nullable type | T |
ReturnType<fn> | Function type | Return type |
Parameters<fn> | Function type | Param tuple |
Awaited<Promise<T>> | Promise type | T |