TypeScript Tutorial: Utility Types
TypeScript's built-in type transformers: Partial, Required, Readonly, Pick, Omit, Record, and more.
Partial\<T\>
Makes all properties optional:
```typescript
interface User {
id: number;
name: string;
email: string;
}
function updateUser(id: number, changes: Partial<User>) {
// changes can have any subset of User's properties
}
updateUser(1, { name: "Bob" }); // ✓
updateUser(1, { email: "b@b.com" }); // ✓
updateUser(1, { name: "Bob", email: "b@b.com" }); // ✓
```
---
Required\<T\>
Makes all optional properties required — the opposite of `Partial`:
```typescript
interface Config {
host?: string;
port?: number;
}
const config: Required<Config> = {
host: "localhost", // now required
port: 3000, // now required
};
```
---
Readonly\<T\>
Makes all properties readonly:
```typescript
const user: Readonly<User> = { id: 1, name: "Alice", email: "a@a.com" };
user.name = "Bob"; // ✗ Error: Cannot assign to 'name'
```
---
Pick\<T, K\>
Create a type with only the specified properties:
```typescript
type UserPreview = Pick<User, "id" | "name">;
// { id: number; name: string }
```
---
Omit\<T, K\>
Create a type with all properties **except** the specified ones:
```typescript
type UserWithoutEmail = Omit<User, "email">;
// { id: number; name: string }
```
---
Record\<K, V\>
Create an object type with specific keys and value type:
```typescript
type Scores = Record<string, number>;
const scores: Scores = { alice: 95, bob: 87, carol: 92 };
type UserRoles = Record<"admin" | "editor" | "viewer", boolean>;
```
---
ReturnType\<T\> and Parameters\<T\>
Extract the return type or parameter types from a function:
```typescript
function fetchUser(id: number): Promise<User> { /* ... */ }
type FetchReturn = ReturnType<typeof fetchUser>; // Promise<User>
type FetchParams = Parameters<typeof fetchUser>; // [id: number]
```
---
What's Next?
Next: **Type Guards & Narrowing** — safely narrow union types at runtime.
What you'll learn in this TypeScript utility types tutorial
This interactive TypeScript tutorial has 12 hands-on exercises. Estimated time: 12 minutes.
- Partial — make everything optional — `Partial<T>` makes all properties optional. Perfect for update/patch operations where only changed fields are sent.
- Required — all fields mandatory — `Required<T>` makes all optional properties required. Use it when a function needs all fields to be present.
- Readonly — prevent mutation — `Readonly<T>` makes all properties readonly. Useful for frozen configuration and immutable data.
- Pick — select specific fields — `Pick<T, K>` creates a type with only the specified keys. Perfect for API responses that should expose only certain fiel…
- Omit — remove specific fields — `Omit<T, K>` creates a type with all fields except the specified ones. Cleaner than `Pick` when you want to keep most fi…
- Record — typed key-value maps — `Record<K, V>` creates an object type with keys of type `K` and values of type `V`. It's exhaustive when `K` is a union …
- ReturnType — extract function return type — `ReturnType<T>` extracts the return type from a function type. Use it to derive types from existing functions without du…
- Parameters — extract function parameter types — `Parameters<T>` extracts the parameter types as a tuple. Combine with `ReturnType` to fully describe a function.
- Exclude and Extract — filter union types — `Exclude<T, U>` removes types from a union. `Extract<T, U>` keeps only matching types. Both filter union members.
- NonNullable — remove null and undefined — `NonNullable<T>` removes `null` and `undefined` from a union. Use it when you've already checked for null.
- ConstructorParameters and InstanceType — `ConstructorParameters<T>` extracts a constructor's parameter types. `InstanceType<T>` gives you the instance type of a …
- Combining utility types — real patterns — Utility types can be combined to express complex transformations. Here are patterns from real codebases.
TypeScript Utility Types concepts covered
- Partial\<T\>
- Required\<T\>
- Readonly\<T\>
- Pick\<T, K\>
- Omit\<T, K\>
- Record\<K, V\>
- ReturnType\<T\> and Parameters\<T\>
- What's Next?