{ ILoveJS }

TypeScript Utility Types Cheatsheet

typescript

All TypeScript built-in utility types with examples.

8 sections · 29 items

Object Property Modifiers

Partial<T>
Partial<Type>

Makes all properties of Type optional

typescript
// Before: { name: string; age: number }
type Result = Partial<User>;
// After: { name?: string; age?: number }
Required<T>
Required<Type>

Makes all properties of Type required

typescript
// Before: { name?: string; age?: number }
type Result = Required<User>;
// After: { name: string; age: number }
Readonly<T>
Readonly<Type>

Makes all properties of Type readonly

typescript
// Before: { name: string; age: number }
type Result = Readonly<User>;
// After: { readonly name: string; readonly age: number }

Object Property Selection

Pick<T, K>
Pick<Type, Keys>

Creates a type with only the specified keys from Type

typescript
// Before: { name: string; age: number; email: string }
type Result = Pick<User, 'name' | 'email'>;
// After: { name: string; email: string }
Omit<T, K>
Omit<Type, Keys>

Creates a type with all properties except the specified keys

typescript
// Before: { name: string; age: number; email: string }
type Result = Omit<User, 'age'>;
// After: { name: string; email: string }
Record<K, T>
Record<Keys, Type>

Creates an object type with keys of type Keys and values of type Type

typescript
// Before: manually defining { [key: string]: User }
type Result = Record<string, User>;
// After: { [x: string]: User }

Union Type Manipulation

Exclude<T, U>
Exclude<UnionType, ExcludedMembers>

Removes types from a union that are assignable to ExcludedMembers

typescript
// Before: 'a' | 'b' | 'c'
type Result = Exclude<'a' | 'b' | 'c', 'a'>;
// After: 'b' | 'c'
Extract<T, U>
Extract<Type, Union>

Extracts types from a union that are assignable to Union

typescript
// Before: 'a' | 'b' | 'c'
type Result = Extract<'a' | 'b' | 'c', 'a' | 'f'>;
// After: 'a'
NonNullable<T>
NonNullable<Type>

Removes null and undefined from Type

typescript
// Before: string | null | undefined
type Result = NonNullable<string | null | undefined>;
// After: string

Function Type Extraction

Parameters<T>
Parameters<Type>

Extracts parameter types from a function type as a tuple

typescript
// Before: (a: string, b: number) => void
type Result = Parameters<typeof fn>;
// After: [a: string, b: number]
ReturnType<T>
ReturnType<Type>

Extracts the return type of a function type

typescript
// Before: () => { name: string; age: number }
type Result = ReturnType<typeof getUser>;
// After: { name: string; age: number }
ConstructorParameters<T>
ConstructorParameters<Type>

Extracts parameter types from a constructor function as a tuple

typescript
// Before: class User { constructor(name: string, age: number) {} }
type Result = ConstructorParameters<typeof User>;
// After: [name: string, age: number]
InstanceType<T>
InstanceType<Type>

Extracts the instance type of a constructor function

typescript
// Before: class User { name: string; }
type Result = InstanceType<typeof User>;
// After: User

This Parameter Types

ThisParameterType<T>
ThisParameterType<Type>

Extracts the type of the this parameter from a function type

typescript
// Before: function fn(this: User, x: number) {}
type Result = ThisParameterType<typeof fn>;
// After: User
OmitThisParameter<T>
OmitThisParameter<Type>

Removes the this parameter from a function type

typescript
// Before: (this: User, x: number) => void
type Result = OmitThisParameter<typeof fn>;
// After: (x: number) => void
ThisType<T>
ThisType<Type>

Marker for contextual this type (no transformation, used with noImplicitThis)

typescript
const obj: ThisType<{ name: string }> & { greet(): void } = {
  greet() { console.log(this.name); }
};

String Manipulation Types

Uppercase<S>
Uppercase<StringType>

Converts string literal type to uppercase

typescript
// Before: 'hello'
type Result = Uppercase<'hello'>;
// After: 'HELLO'
Lowercase<S>
Lowercase<StringType>

Converts string literal type to lowercase

typescript
// Before: 'HELLO'
type Result = Lowercase<'HELLO'>;
// After: 'hello'
Capitalize<S>
Capitalize<StringType>

Converts first character of string literal type to uppercase

typescript
// Before: 'hello'
type Result = Capitalize<'hello'>;
// After: 'Hello'
Uncapitalize<S>
Uncapitalize<StringType>

Converts first character of string literal type to lowercase

typescript
// Before: 'Hello'
type Result = Uncapitalize<'Hello'>;
// After: 'hello'

Promise and Async Types

Awaited<T>
Awaited<Type>

Recursively unwraps Promise types to get the resolved value type

typescript
// Before: Promise<Promise<string>>
type Result = Awaited<Promise<Promise<string>>>;
// After: string
Awaited with async function
Awaited<ReturnType<typeof asyncFn>>

Get the resolved type of an async functions return value

typescript
async function fetchUser() { return { name: 'John' }; }
type Result = Awaited<ReturnType<typeof fetchUser>>;
// { name: string }
Awaited with union
Awaited<Type | Promise<Type>>

Handles union types with mixed Promise and non-Promise types

typescript
// Before: string | Promise<number>
type Result = Awaited<string | Promise<number>>;
// After: string | number

Common Utility Patterns

DeepPartial Pattern
type DeepPartial<T> = { [P in keyof T]?: DeepPartial<T[P]> }

Custom utility making all nested properties optional recursively

typescript
type Nested = { user: { name: string; addr: { city: string } } };
type Result = DeepPartial<Nested>;
// All levels become optional
Partial + Pick Combo
Partial<Pick<Type, Keys>>

Make only specific properties optional

typescript
type User = { name: string; age: number; email: string };
type Result = Partial<Pick<User, 'age' | 'email'>>;
// { age?: number; email?: string }
Required + Omit Combo
Required<Omit<Type, Keys>>

Make all properties required except excluded ones

typescript
type User = { name?: string; age?: number; id: string };
type Result = Required<Omit<User, 'id'>>;
// { name: string; age: number }
Record with Union Keys
Record<'key1' | 'key2', Type>

Create object with specific literal keys

typescript
type Status = 'pending' | 'done' | 'error';
type StatusMap = Record<Status, { label: string }>;
// { pending: {...}; done: {...}; error: {...} }
Extract + keyof Pattern
Extract<keyof Type, string>

Get only string keys from an object type

typescript
type Obj = { 0: 'a'; name: 'b'; [Symbol.iterator]: 'c' };
type Result = Extract<keyof Obj, string>;
// 'name'
NonNullable + Array Element
NonNullable<Type[number]>

Get non-nullable element type from an array that may contain nulls

typescript
type Arr = (string | null | undefined)[];
type Result = NonNullable<Arr[number]>;
// string

Related Content