Codectionary / Developer documentation / TypeScript

Partial<T> and Required<T>

Partial<T> constructs a new type with every property of T marked optional - useful for representing partial updates, like a PATCH request body. Required<T> does the opposite, making every property mandatory even if the original type had optional ones, useful for ensuring a fully-populated object at a specific point in your code.

Syntax

Partial<Type>
Required<Type>

Examples

Partial<T> for Update Functions

A very common real-world use case - allowing partial updates to an object.

interface User {
  id: number;
  name: string;
  email: string;
}

function updateUser(id: number, updates: Partial<User>): void {
  // updates can include ANY subset of User's properties
  console.log(`Updating user ${id}:`, updates);
}

updateUser(1, { name: "New Name" });          // OK - only updating name
updateUser(1, { email: "new@example.com" });   // OK - only updating email

Required<T> to Enforce Completeness

Ensuring an object has every property populated, even ones that started optional.

interface Config {
  host?: string;
  port?: number;
  timeout?: number;
}

function startServer(config: Required<Config>): void {
  // every property is guaranteed to be present here, not optional
  console.log(`Starting on ${config.host}:${config.port}`);
}

// startServer({ host: "localhost" }); // Error - port and timeout are missing
startServer({ host: "localhost", port: 3000, timeout: 5000 }); // OK

Best practices

  • Use Partial<T> for update/patch functions where the caller should be able to supply any subset of an object's fields
  • Use Required<T> at boundaries where a fully-populated object is genuinely necessary, even if the original type allows optional fields elsewhere
  • Avoid using Partial<T> for creating brand-new objects when most fields are actually required - it can hide missing-field bugs that a stricter type would catch
  • Combine Partial<T> with a spread merge pattern ({ ...existing, ...updates }) for a clean, type-safe update implementation

At a glance

Purpose
Static types for JavaScript
File extension
.ts ยท .tsx
Runs in
Compiled to JavaScript
Usually used with
JavaScript and its ecosystem

Specifications & further reading

Related TypeScript documentation