# Partial & Required

> Toggle all properties as optional or required.

Subject: TypeScript | Lesson 36 of 54 | 4 min read
Interactive lesson: https://codewithanimation.in/typescript/partial-and-required
Previous: [Mutability Limits](https://codewithanimation.in/typescript/mutability-limits.md) | Next: [The Record Type](https://codewithanimation.in/typescript/record-type.md)

## Batch Modifiers

`Partial<T>` takes an object type and returns a new type with every property marked as optional (`?`).

`Required<T>` does the exact opposite: it takes an object type and returns a new type with all optional modifiers stripped away.

## The PATCH Scenario

Imagine a `User` type with 20 properties, all required to create a new user. But when you want to update a user (a PATCH request), you only need to send the fields that changed.

Instead of writing a second `UpdateUserDTO` interface where everything is optional, you can simply use `Partial<User>`.

## Syntax

```typescript
interface Todo {
  title: string;
  description: string;
}

// Updating a Todo: we only need to provide some fields
function updateTodo(todo: Todo, fieldsToUpdate: Partial<Todo>) {
  return { ...todo, ...fieldsToUpdate };
}

// We can just pass the title!
const updated = updateTodo(todo, { title: "New Title" });
```

## Common mistake: Shallow by Default

Both `Partial` and `Required` are **shallow**. They only apply the modifier to the top-level properties of the object.

If you have a nested object, like `user.address.street`, the `address` itself becomes optional, but if you *do* provide an address, `street` is still strictly required!

## Tip: Under the Hood

These utilities are just mapped types! `Partial` uses `[P in keyof T]?: T[P]`.

`Required` uses the `-?` syntax to actively remove optional flags: `[P in keyof T]-?: T[P]`.

## Try it: Try it

Toggle the utility wrapper between `Partial<T>` and `Required<T>` to see how the compiler applies or removes the `?` modifier across all properties simultaneously.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/typescript/partial-and-required

## Check yourself

### Question 1

If `type User = { name: string, profile?: { age: number } }`, what happens to `age` when you use `Required<User>`?

- A. It remains optional.
- B. It becomes required.
- C. It throws a compiler error.

Correct answer: A

Required<T> is shallow. It will make the top-level 'profile' property required, but it does not recurse into nested objects to make 'age' required.

- Why not B: Required is shallow. It only affects top-level properties.
- Why not C: It just ignores nested properties.

## Remember this

- `Partial<T>` makes all top-level properties optional.
- `Required<T>` makes all top-level properties required.
- They are shallow—they do not affect nested objects.
- They are implemented using mapped types under the hood.
