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

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Make a TypeScript parameter optional by adding ? after its name: greeting?: string. The caller may then omit that argument, and the function receives undefined when it does. Use a default initializer instead when omission should trigger a fallback value.

How to declare an optional parameter

Function parameters are required unless you mark them optional. Add ? after the parameter name and before the type annotation:

function greet(name: string, greeting?: string): string {
  return greeting ? `${greeting}, ${name}` : `Hello, ${name}`;
}

greet("Ada");
greet("Ada", "Welcome");

Because greeting may be absent, the function must account for it being undefined. Here, the conditional chooses a greeting when one is supplied and otherwise uses the built-in message.

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

What happens when an optional argument is omitted?

At runtime, JavaScript gives an unprovided parameter the value undefined. TypeScript models that behavior: with strictNullChecks enabled, code must treat an optional parameter as possibly undefined. Optionality does not permit null; pass null only if the declared type explicitly allows it. See the TypeScript Handbook’s function documentation.

Optional parameters versus default parameters

Use parameter?: Type when the argument may be omitted and the function should decide how to handle the resulting undefined. Use a default initializer when omission—or an explicit undefined argument—should supply a particular value.

function greet(name: string, greeting = "Hello"): string {
  return `${greeting}, ${name}`;
}

greet("Ada");
greet("Ada", undefined); // uses "Hello"

The initializer controls runtime behavior. The function’s exposed type indicates that the parameter may be omitted, but it does not include the default value itself. The Handbook explains both forms in its functions guide.

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

Can an optional parameter come before a required one?

A parameter marked with ? must follow required parameters. A parameter with a default initializer can appear earlier, but positional arguments still matter: to pass a later required argument, the caller must fill the earlier slot, often with undefined.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
function makeLabel(prefix = "Item", id: number): string {
  return `${prefix} ${id}`;
}

makeLabel(undefined, 42);

If callers regularly need to skip an earlier argument to provide a later one, consider changing the parameter order or accepting a clearly named options object instead. This avoids making callers supply undefined just to reach the argument they need. The rules for optional, default, and rest parameters are described in the TypeScript Handbook.

How optionality works in callback types

In a callback type, an optional parameter means the function invoking the callback may leave that argument out. It does not mean that callback implementations are free to ignore the argument. An implementation can accept fewer parameters than the callback type declares, so keep an argument required when the caller always supplies it.

type EachCallback<T> = (value: T, index: number) => void;

function each<T>(items: T[], callback: EachCallback<T>): void {
  items.forEach((value, index) => callback(value, index));
}

each(["a", "b"], value => console.log(value));

The callback contract says each supplies both a value and an index. The callback passed in the example accepts only the value because it has no need to use the index.

If the function might invoke the callback without an index, declare that possibility in the callback type:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
type MaybeIndexedCallback<T> = (value: T, index?: number) => void;

Now a callback implementation must account for index being undefined. The TypeScript documentation states: “When writing a function type for a callback, never write an optional parameter unless you intend to call the function without passing that argument”. Read the Handbook’s callback guidance for the full explanation.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

When to use a rest parameter instead

An optional parameter allows one argument to be omitted; it does not let callers provide an unlimited number of arguments. For a variable number of values, use a rest parameter. The function receives the remaining arguments as an array:

function logAll(...values: string[]): void {
  values.forEach(value => console.log(value));
}

logAll("first", "second", "third");

For additional context on function compatibility and callback behavior, see the TypeScript documentation on type compatibility.

Choose the right parameter shape

Need Signature approach What it means
A trailing argument may be left out, with no universal fallback value?: T The function must handle undefined.
Omission or explicit undefined should select a fallback value = fallback The initializer supplies the value.
An argument may be skipped before a later required argument Redesign the order or use an options object A default in the earlier position still occupies its slot.
The number of arguments is unbounded ...values: T[] The function receives the remaining values in an array.
A callback always receives an argument, but some implementations do not use it Keep the callback parameter required Implementations can accept fewer parameters without making the contract optional.

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.