October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Use Optional Parameters in TypeScript Interfaces

Add ? after a property or argument name to make it optional in a TypeScript interface. Here is how the two forms differ and how to handle undefined, defaults and null.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Put a ? after the name. For an object property, write width?: number. For a method or call signature in an interface, write resize(width?: number): void. The first makes a property optional. The second makes an argument omittable. Both are covered in the TypeScript Handbook on interfaces and functions.

Optional properties vs. optional parameters

interface SearchOptions {
  query: string;
  limit?: number;      // property may be absent
}

interface SearchService {
  search(query: string, limit?: number): string[];  // argument may be omitted
}

Both declarations use the same syntax but describe different things. In SearchOptions, an object without limit is valid. In SearchService, a call such as service.search("ts") is valid. (Interfaces, More on Functions)

What the implementation receives

An omitted optional parameter is undefined. The Handbook puts it this way: “Although the parameter is specified as type number, the x parameter will actually have the type number | undefined because unspecified parameters in JavaScript get the value undefined.” (TypeScript documentation, More on Functions, “Optional Parameters”). With strict null checking on, the compiler makes you deal with that. See also Advanced Types.

Option 1: Handle absence with a fallback

function search(query: string, limit?: number): string[] {
  const actualLimit = limit ?? 20;
  return [];
}

?? replaces only undefined (and null, if the type allows it). A valid value such as 0 is kept, which || would not do.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Option 2: Use a default parameter

function search(query: string, limit = 20): string[] {
  return [];
}

The default applies when the caller omits the argument or passes undefined. The default value does not appear in the function’s type; the parameter is shown as optional. Defaults belong in implementations, not in interface declarations, so the interface still says limit?: number.

Option 3: Use an options object

When a function has several independent optional settings, a named object is usually clearer than a row of optional arguments:

Rank #2
TypeScript Programming Language - Software Engineer & Coder T-Shirt
  • TypeScript implements a superset of syntax for strictly typed development, facilitating deep static analysis and enhanced development environment integration. The compiler translates source into standard script formats, ensuring parity across any runtime.
  • TypeScript is ideal for front-end developers, full-stack engineers, and software architects who build large-scale web applications. It serves those looking to improve code excellence, reduce bugs through static checking, and maintain complex projects more.
  • Lightweight, Classic fit, Double-needle sleeve and bottom hem
interface SearchOptions {
  limit?: number;
  caseSensitive?: boolean;
}
function search(query: string, options: SearchOptions = {}): string[] {
  const { limit = 20, caseSensitive = false } = options;
  return [];
}
Approach Caller may omit? Fallback built in? Best for
limit?: number argument Yes No; you handle undefined One trailing, truly optional value
limit = 20 default Yes Yes Omission should mean a specific value
Options object with optional properties Yes, per property Via destructuring defaults Many independent settings
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Common mistakes

Assuming optional means nullable

timeoutMs?: number admits undefined but, under strict null checking, not null. If null is a legitimate input, write timeoutMs?: number | null. (Advanced Types)

Placing optional parameters before required ones

Optional parameters normally go after required ones, so callers can drop trailing arguments. If you have many optional values, switch to an options object.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Marking callback parameters optional for convenience

In a callback type like (value: string, index?: number) => void, the ? says the caller of the callback may invoke it without index. If you always pass both, declare index: number. Consumers can still supply a function that uses only the first argument. The Handbook’s Do’s and Don’ts advises against optional callback parameters unless the callback truly may be called without them.

Treating a missing property and an explicit undefined as the same

By default, TypeScript allows { limit: undefined } for limit?: number. Since TypeScript 4.4, the exactOptionalPropertyTypes compiler option tightens this for optional properties, so explicitly assigning undefined is flagged unless the type includes it. It is opt-in, so behavior depends on your tsconfig. (TypeScript 4.4 release notes)

The snippets here follow the Handbook’s documented behavior; they were not compiled against a specific compiler version for this article, so check them against your project’s TypeScript version and strictness settings.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.