Skip to content
jshacks.dev
TypeScript

The Partial Utility Type in TypeScript, With Examples

Partial makes every property of a type optional. See how it works, where it fits in update functions, why it is shallow, and the errors it reports.

7 min read
Summarize in AI
Flat illustration of an editor window full of code, with the TypeScript, JavaScript, Vite and npm logos.

Partial<T> takes a type and returns a new type in which every property is optional. It is one of the most used helpers in TypeScript, because so many real tasks involve an object that is only partly filled in: a patch sent to an API, a set of options that override defaults, a form that is being edited. This post shows how Partial works, writes it out by hand so there is no magic left, covers its limits, including the fact that it only goes one level deep, and gives patterns for using it safely. All examples target TypeScript 6.x, and the reference is the Partial entry in the utility types handbook page.

The basic idea

Start with a type that has required properties.

user.tsTypeScript
interface User {
  id: number;
  name: string;
  email: string;
}

const draft: Partial<User> = {
  name: "Ada",
};

console.log(draft);
Text
{ name: 'Ada' }

Without Partial, the assignment would fail because id and email are missing. With it, any subset of the properties is allowed, including none at all. The properties that are present must still have the right types.

wrong-type.tsTypeScript
interface User {
  id: number;
  name: string;
  email: string;
}

const draft: Partial<User> = {
  name: 1,
};
Text
wrong-type.ts(8,3): error TS2322: Type 'number' is not assignable to type 'string'.

So Partial relaxes presence, and not types. It is not a way to turn off checking.

How it is defined

Partial is not built into the language syntax. It is a mapped type in the standard library, and writing it yourself shows exactly what it does.

my-partial.tsTypeScript
type MyPartial<T> = {
  [P in keyof T]?: T[P];
};

interface Point {
  x: number;
  y: number;
}

const origin: MyPartial<Point> = {};
const onlyX: MyPartial<Point> = { x: 5 };

console.log(origin, onlyX);
Text
{} { x: 5 }

The mapped type loops over every key of T with P in keyof T, keeps each property's type with T[P], and adds the ? modifier to make it optional. That is the whole idea, and the mapped types page in the handbook covers the syntax. Understanding this definition also explains the limits below, since the mapping touches only the top-level keys.

The update function pattern

The most common use is a function that updates an object with a patch. The patch type says "some of the fields of a User".

update.tsTypeScript
interface User {
  id: number;
  name: string;
  email: string;
}

function updateUser(user: User, patch: Partial<User>): User {
  return { ...user, ...patch };
}

const ada: User = { id: 1, name: "Ada", email: "[email protected]" };

console.log(updateUser(ada, { name: "Ada Lovelace" }));
console.log(updateUser(ada, {}));
Text
{ id: 1, name: 'Ada Lovelace', email: '[email protected]' }
{ id: 1, name: 'Ada', email: '[email protected]' }

The function returns a full User, because spreading a partial over a complete object leaves the complete properties in place. TypeScript can verify that: the spread result has every property of User, so the return type is satisfied. An empty patch is valid and returns a copy.

There is a subtlety that catches people. A Partial<User> can legitimately contain a property explicitly set to undefined. With the spread above, that overwrites the existing value.

undefined-patch.tsTypeScript
interface User {
  id: number;
  name: string;
}

function updateUser(user: User, patch: Partial<User>): User {
  return { ...user, ...patch };
}

const ada: User = { id: 1, name: "Ada" };
const result = updateUser(ada, { name: undefined });

console.log(result);
Text
{ id: 1, name: undefined }

This compiles, which is a hole in the types: result.name is typed string but holds undefined. The compiler option exactOptionalPropertyTypes closes the hole by forbidding an explicit undefined unless the property type includes it. Without that option, filter out undefined values in the function, or write the merge so that it skips them.

Partial for options and defaults

Another popular use is an options object that overrides defaults. The function takes Partial of the options, and merges it over the defaults, which are complete.

options.tsTypeScript
interface Options {
  retries: number;
  timeoutMs: number;
  verbose: boolean;
}

const defaults: Options = { retries: 3, timeoutMs: 1000, verbose: false };

function configure(overrides: Partial<Options> = {}): Options {
  return { ...defaults, ...overrides };
}

console.log(configure({ retries: 5 }));
console.log(configure());
Text
{ retries: 5, timeoutMs: 1000, verbose: false }
{ retries: 3, timeoutMs: 1000, verbose: false }

Callers pass only what they care about, and everything inside the function sees a complete Options. This keeps the public signature friendly while the internal code never has to handle missing values.

Partial is shallow

The mapping applies only to the first level of properties. Nested objects stay required.

shallow.tsTypeScript
interface Theme {
  color: string;
  size: number;
}

interface Settings {
  theme: Theme;
  locale: string;
}

const patch: Partial<Settings> = {
  theme: { color: "red" },
};
Text
shallow.ts(12,3): error TS2741: Property 'size' is missing in type '{ color: string; }' but required in type 'Theme'.

theme itself became optional, but when you supply it, it must be a complete Theme. If you need a deep version, you can write a recursive mapped type.

deep.tsTypeScript
type DeepPartial<T> = {
  [P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P];
};

interface Theme {
  color: string;
  size: number;
}

interface Settings {
  theme: Theme;
  locale: string;
}

const patch: DeepPartial<Settings> = {
  theme: { color: "red" },
};

console.log(patch);
Text
{ theme: { color: 'red' } }

This simple version treats every object type, including arrays and functions, as something to recurse into, which is rarely what you want for them. Real projects usually add exceptions or use a well-tested helper from a library. Deep partials also raise a design question, because a deep patch needs a deep merge at runtime, and a plain spread does not do that.

Combining with other utility types

Partial combines well with its siblings. To make only some properties optional, pair it with Pick and Omit.

combine.tsTypeScript
interface User {
  id: number;
  name: string;
  email: string;
}

type UserPatch = Partial<Pick<User, "name" | "email">>;

const ok: UserPatch = { email: "[email protected]" };
const bad: UserPatch = { id: 2 };
Text
combine.ts(10,26): error TS2353: Object literal may only specify known properties, and 'id' does not exist in type 'Partial<Pick<User, "name" | "email">>'.

The patch type allows name and email to be changed and rejects id, which is what most update endpoints want: a record's identity should not be editable. Another combination is making everything optional except one field, using an intersection such as Partial<User> & Pick<User, "id">, which describes a patch that must carry the id.

Testing the types

Types can be tested too. A pair of assignments serve as compile-time assertions, and a test that is meant to fail can be marked with a comment that the compiler checks.

type-test.tsTypeScript
interface User {
  id: number;
  name: string;
}

const accepted: Partial<User> = {};

// @ts-expect-error id must be a number
const rejected: Partial<User> = { id: "1" };

console.log(accepted, typeof rejected);
Text
{} object

The @ts-expect-error comment tells the compiler that the next line must contain an error. If a later change to the type makes the line valid, the compiler reports an unused directive, so the test fails loudly. This keeps type behavior from changing silently.

When not to use Partial

Partial is convenient and easy to overuse. A function that accepts Partial<User> and then reads user.email.length has to deal with undefined, and every place that consumes a partial pays that cost. Use it for inputs, such as patches and overrides, and convert to a full type as early as possible. Do not use it to avoid defining a second type when the two shapes are genuinely different. A creation payload, for example, may need fewer fields than the stored record and more than a patch, and a named type with a name that explains its purpose is clearer than a long chain of utility types.

Also remember that Partial is a compile-time tool. It does not validate data that arrives at runtime. A JSON body sent to your server has to be checked by a validator before you trust it to match Partial<User>.

Partial in function arguments and return types

Because Partial describes "some of these fields", it also shows up in return types of functions that may not be able to produce everything. A parser that extracts what it can from a messy input might return Partial<Record> and let the caller decide what is missing. That is honest, but it pushes checking onto every caller, so the better design is usually to validate, then return the full type or an error.

A good question to ask before you write Partial<T> in a signature is who fills in the missing fields. For a patch, the answer is the function itself, by merging. For options, the answer is the defaults. If nobody fills them in, the optional fields are a risk for the reader of the code, and a more specific type will serve better.

Share

Keep reading

Flat illustration of two options weighed side by side, with the TypeScript, Node.js, npm and JavaScript logos.
TypeScript

Type Annotations vs Type Assertions

A type annotation asks the compiler to check a value, while an assertion tells it to trust you. Learn the difference, the errors and safer alternatives.

6 min read
Flat illustration of an editor window full of code, with the TypeScript, npm and Node.js logos.
TypeScript

Type Inference in TypeScript

TypeScript works out most types for you. Learn how inference treats let and const, arrays, return values and callbacks, and when to write a type yourself.

7 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.