Optional and Rest Parameters
Not every argument is needed every time.
An argument is a value you pass into a function.
TypeScript supports optional parameters, defaults, and rest parameters.
Your function shapes can match real calls and still stay safe.
Optional parameters
Mark a parameter with ?.
Optional ones must come after required ones.
Inside the function, treat them as maybe undefined.
Undefined means nothing was passed.
Default parameters
Defaults make a parameter optional.
They fill in a value when the caller skips it.
TypeScript can guess the type from the default.
Rest parameters
A rest parameter gathers leftover arguments into an array.
Rest means take the remaining pieces.
Type it as an array: ...scores: number[].
Optional fields in an options object
A common pattern is one options object with optional fields.
That is cleaner than a long list of maybe-arguments in order.
Callers can name only the knobs they care about.
Putting it together
Use defaults for simple fill-ins.
Use an options object when there are many knobs.
Use rest when the count really varies.
Use defaults and options objects for readable APIs. Use rest when the count really varies.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.