Mapping Types

Partial<T>

Constructs a type with all properties of T set to optional, allowing you to provide only a subset of the properties.

Definition

TypeScript
type Partial<T> = {
  [P in keyof T]?: T[P];
};

Examples

Basic Usage
interface User {
  name: string;
  age: number;
  email: string;
}

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

const user: User = { name: "Alice", age: 30, email: "[email protected]" };
const updated = updateUser(user, { age: 31 });
Config Defaults
interface Config {
  host: string;
  port: number;
  debug: boolean;
  timeout: number;
}

const defaults: Config = { host: "localhost", port: 3000, debug: false, timeout: 5000 };

function createConfig(overrides: Partial<Config>): Config {
  return { ...defaults, ...overrides };
}

const config = createConfig({ port: 8080, debug: true });
Form State
interface FormData {
  username: string;
  password: string;
  rememberMe: boolean;
}

type FormDraft = Partial<FormData>;

const draft: FormDraft = { username: "alice" };
// All other fields are optional during editing

Common Use Cases

  • 1Update functions that accept a subset of fields
  • 2Default configuration objects with overrides
  • 3Partial form state during incremental data entry
  • 4Builder patterns where properties are set step by step
  • 5Patch requests in REST APIs

Understanding Partial<T>

Partial<T> is one of the most commonly used utility types in TypeScript. It takes an existing type T and creates a new type where every property becomes optional. Under the hood, Partial uses a mapped type that iterates over each key in T with keyof and appends the ? modifier, making each property no longer required.

This utility type is invaluable when working with update or patch operations. For example, when you have a function that modifies an entity in a database, you typically want to accept only the fields that should change rather than requiring the entire object. Partial<T> makes this pattern type-safe without defining a separate interface.

Another common use case is configuration merging. You can define a complete Config interface with all required fields, set sensible defaults, and then allow callers to pass Partial<Config> to override only what they need. The spread operator then cleanly merges the override into the defaults.

Partial also shines in form handling scenarios where a user fills in data incrementally. Before submission, the form state is naturally a partial version of the final validated shape. By typing intermediate state as Partial<FormData>, you get type safety during editing without premature required-field errors.

Note that Partial only affects top-level properties. If you need deep partial behavior where nested objects are also made optional recursively, you will need a custom DeepPartial type. Libraries like ts-toolbelt provide such types out of the box.

Related Types

More Mapping Types

Explore TypeScript Types

Browse our complete reference of 30 TypeScript utility types with definitions, examples, and explanations.