TypeScript Functions and Narrowing/Use optional parameters

Mark a parameter as optional with `?`.

Section: Functions and Parameters

Use optional parameters

typescript
typescript
function greet(name: string, prefix?: string) {
  return `${prefix ?? "Hello"}, ${name}`;
}
Explanation

Optional parameters become `T | undefined` in the body.

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing Functions and Parameters
Type a function
Annotate parameters and return values.
OpenIn sheettypescriptsame section
Use default parameters
Provide a default value and keep a concrete type.
OpenIn sheettypescriptsame section
Use rest parameters
Accept a variable number of arguments.
OpenIn sheettypescriptsame section
Use a function type alias
Describe callback signatures explicitly.
OpenIn sheettypescriptsame section
Declare function overloads
Expose multiple call signatures with one implementation.
OpenIn sheettypescriptsame section
Narrow with `typeof`
Refine primitive unions using runtime checks.
OpenIn sheettypescript1 tag match