Skip to content
jshacks.dev
TypeScript

The Required Utility Type in TypeScript, With Examples

Required makes every optional property of a type mandatory. See how it is defined, where it helps after defaults are applied, and the errors it reports.

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

Required<T> is the opposite of Partial<T>. It takes a type with optional properties and produces a type in which every property must be present. That sounds like a niche tool, but it solves a very common problem: you accept loose input from callers, fill in the gaps with defaults, and then want the rest of your code to rely on the fact that nothing is missing. Required expresses that "after defaults" shape without writing the type twice. This post shows how it works, how it is defined, the role of the -? modifier, its relation to undefined, and the places where it falls short. The examples use TypeScript 6.x, and the reference is the Required entry in the utility types page.

The basic idea

Take a type where some properties are optional, and wrap it in Required.

config.tsTypeScript
interface Config {
  host: string;
  port?: number;
  debug?: boolean;
}

const strict: Required<Config> = {
  host: "localhost",
  port: 8080,
  debug: false,
};

console.log(strict);
Text
{ host: 'localhost', port: 8080, debug: false }

In Config, only host is mandatory. In Required<Config>, all three are. Leaving one out is an error that names the missing property.

missing.tsTypeScript
interface Config {
  host: string;
  port?: number;
  debug?: boolean;
}

const strict: Required<Config> = {
  host: "localhost",
  port: 8080,
};
Text
missing.ts(7,7): error TS2741: Property 'debug' is missing in type '{ host: string; port: number; }' but required in type 'Required<Config>'.

The message tells you the exact type the compiler inferred for your literal and the type it wanted. That is a handy way to see what Required<Config> demands.

How it is defined

Required is a mapped type with one unusual piece of syntax, the minus sign in front of the question mark.

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

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

const p: MyRequired<Point> = { x: 1, y: 2 };

console.log(p);
Text
{ x: 1, y: 2 }

The -? means "remove the optional modifier". The prefix works for modifiers in both directions: ? or +? adds optionality, -? removes it, and the same is true of readonly and -readonly. The mapped types page covers the full list of modifier prefixes. With -?, the compiler also strips undefined from the property type when it came from the optional marker.

The defaults pattern

The most useful application is a function that accepts a loose options object, applies defaults and then works with a complete one.

defaults.tsTypeScript
interface ServerOptions {
  host?: string;
  port?: number;
  secure?: boolean;
}

const defaults: Required<ServerOptions> = {
  host: "localhost",
  port: 3000,
  secure: false,
};

function resolveOptions(options: ServerOptions = {}): Required<ServerOptions> {
  return { ...defaults, ...options };
}

const resolved = resolveOptions({ port: 8080 });

console.log(resolved);
console.log(resolved.port.toFixed(0));
Text
{ host: 'localhost', port: 8080, secure: false }
8080

Three things happen here. The defaults constant is typed as Required<ServerOptions>, so if someone adds a new option to the interface and forgets to give it a default, the compiler reports it right there. The function accepts the loose type and returns the strict one. And callers of resolveOptions can call resolved.port.toFixed(0) without a null check, since the type no longer includes undefined.

That first point is the real benefit. The pairing of ServerOptions and Required<ServerOptions> keeps defaults complete as the interface grows, without a second hand-written type.

Required and undefined

Required removes the optional marker, and it does not change properties whose type explicitly includes undefined. The distinction matters.

explicit-undefined.tsTypeScript
interface Profile {
  nickname?: string;
  bio: string | undefined;
}

const profile: Required<Profile> = {
  nickname: "ada",
  bio: undefined,
};

console.log(profile);
Text
{ nickname: 'ada', bio: undefined }

Here nickname becomes a required string, and bio stays a required property whose value may be undefined. A property that must exist but can hold undefined is a different thing from a property that may be absent, and TypeScript tracks them separately. With the exactOptionalPropertyTypes option on, the difference is enforced more strictly for optional properties.

Required is shallow

Like Partial, Required works only on the top-level properties.

shallow.tsTypeScript
interface Address {
  street?: string;
  city?: string;
}

interface Customer {
  name?: string;
  address?: Address;
}

const customer: Required<Customer> = {
  name: "Grace",
  address: { city: "New York" },
};

console.log(customer.address.city);
Text
New York

The code compiles. The address property is now mandatory, but its own street stays optional, so the literal with only a city is accepted. If you want the nested shape to be strict as well, say so in the type of the property.

nested.tsTypeScript
interface Address {
  street?: string;
  city?: string;
}

interface CustomerRecord {
  name: string;
  address: Required<Address>;
}

const customer: CustomerRecord = {
  name: "Grace",
  address: { city: "New York" },
};
Text
nested.ts(13,3): error TS2741: Property 'street' is missing in type '{ city: string; }' but required in type 'Required<Address>'.

Applying Required at each level you care about is explicit and easy to read. A fully recursive version is possible with a mapped type, but it must treat arrays, functions and optional properties carefully, so most projects prefer to name the nested types they want strict.

Requiring only some properties

Often you want a few fields to become required and the rest to stay as they are. Combine Required with Pick in an intersection.

some.tsTypeScript
interface Draft {
  id?: number;
  title?: string;
  body?: string;
}

type Publishable = Draft & Required<Pick<Draft, "title" | "body">>;

const ready: Publishable = { title: "Hello", body: "World" };
const notReady: Publishable = { title: "Hello" };
Text
some.ts(10,7): error TS2741: Property 'body' is missing in type '{ title: string; }' but required in type 'Required<Pick<Draft, "title" | "body">>'.

Publishable keeps id optional while requiring title and body. The first constant passes and the second is rejected. This pattern models workflow stages well: a draft with everything optional, and a publishable value with the fields that publishing needs.

Narrowing at runtime to match the type

A type that says "all required" has to be backed by code that makes it true. A type guard can verify the shape when you cannot trust the source.

guard.tsTypeScript
interface Options {
  retries?: number;
  label?: string;
}

function isComplete(options: Options): options is Required<Options> {
  return options.retries !== undefined && options.label !== undefined;
}

function describe(options: Options): string {
  if (isComplete(options)) {
    return `${options.label} x${options.retries.toFixed(0)}`;
  }
  return "incomplete";
}

console.log(describe({ retries: 2, label: "sync" }));
console.log(describe({ label: "sync" }));
Text
sync x2
incomplete

Inside the if, TypeScript treats options as Required<Options>, so options.retries.toFixed(0) needs no extra check. The guard function is the place where the promise in the type is actually verified. If you write as Required<Options> instead, the compiler believes you and nothing at runtime confirms it.

Testing the types

A compile-time test can pin down the behavior, using a directive that the compiler verifies.

type-test.tsTypeScript
interface Options {
  retries?: number;
}

const complete: Required<Options> = { retries: 1 };

// @ts-expect-error retries is required here
const empty: Required<Options> = {};

console.log(complete.retries, typeof empty);
Text
1 object

If someone later changes Required usage so that the empty object is accepted, the @ts-expect-error line becomes an unused directive, and the compiler reports it. The test therefore fails when the behavior changes.

When not to use Required

Required is the right tool when you have genuinely applied defaults, or validated, and want the type to say so. Do not use it to silence optional property errors in code that has not filled the values in. If a function is handed a Config and does Required<Config> as a cast, the compiler stops warning while undefined values can still arrive at runtime. In that case, handle the optional properties explicitly, or fill them with defaults first.

Also consider whether a separate named interface says more. When the strict shape is used widely, and has its own meaning such as ResolvedConfig, a named type documents the intent better than Required<Config> repeated across the codebase. A type alias, type ResolvedConfig = Required<Config>, gives you the name while keeping the single source of truth.

Required in libraries and public APIs

Libraries often expose two shapes of the same configuration: the loose one that users pass, and the resolved one used inside. Naming them separately helps readers. A public Options type with optional fields is friendly, and an internal ResolvedOptions = Required<Options> states the post-defaults guarantee. Internal functions then take the resolved type, so they cannot be called with raw user input by mistake, and a missing default shows up as a compile error at the point where the defaults object is defined.

This split also helps documentation. The public type lists what users may change, and the resolved type shows what the library will use. When you add an option, you touch the interface once, and the compiler walks you to the default that has to be added.

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.