Free tools Windows power users keep installed
One-click scans. No signup required.
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.
#1 Best Overall
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 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 |
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.
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.
Quick Recap
Best Value
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.
The Tool Desk
Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →




