Terminology (5)Terms used in this lesson
Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.
- Variadic parameter
- An interface accepting a variable number of trailing arguments; arrays, slices and compile-time packs represent them differently.
- Default value
- A value used for a parameter when the caller leaves the argument out.
- Not given
- A call that leaves an optional value out, as opposed to passing 0 or an empty value.
- Spreading
- Passing a list’s elements as separate arguments rather than the list as one.
- Rest parameter
- TypeScript’s ...name: T[] parameter; the call-site ... spreads an array into it.
Optional inputs and variable argument lists
TypeScript rest parameters collect trailing arguments into an array, while call-site spread supplies iterable elements as separate arguments. A prepared array and a sequence of arguments therefore cross different call boundaries. Check which interface the receiving function declares.
A default parameter supplies a fallback under JavaScript’s default-initialization rules. Optional input also needs a representation that preserves whether the caller supplied a value. Keep that presence decision separate from validating the value and from forwarding a prepared payload.
01Any number of values
A call with no extra values is a normal call
A match-report worker accepts a variable-length score submission for each player. After preparing a summary, it needs a delivery-job attempt option and an ordered batch of channel notices. Those inputs arrive as lists and optional fields; the worker must preserve what the caller supplied as it builds its report.
A submitted score list can have several entries or no entries yet. Before computing the player summary, isolate that empty-list boundary with total, which adds the supplied points. It receives three values, no values, and a two-value list. Predict the second line.
total has a rest parameter, ...points: number[]; the last call spreads an array with ...extra.
function total(...points: number[]): number {
let sum = 0;
for (const point of points) {
sum += point;
}
return sum;
}
const extra = [4, 3];
console.log(total(12, 9, 15));
console.log(total());
console.log(total(...extra));
npx tsc --noEmit --strict main.ts && node main.ts
Use the selected language’s setup from the first reading and replace the file for each experiment. Scores and attempt counts are supplied small nonnegative whole numbers. Messages are single input words. The programs print the summary and dispatch report for these valid inputs.
02Left out versus zero
Resolve the job’s attempt option
The worker’s policy allows 3 attempts when a job omits its count; a supplied zero disables attempts. Compare how each shown interface represents that option. Predict the call that supplies zero.
attempts declares count = 3, a default parameter.
function attempts(count = 3): number {
return count;
}
console.log(attempts());
console.log(attempts(undefined));
console.log(attempts(0));
03Passing a list on
Forward the prepared notice batch
The notice stage already holds its messages in a list. To inspect the next call boundary without printing the messages, count reports how many arguments it receives. Compare a direct call with the call through that prepared list.
forward collects its arguments with ...items, then calls count(items).
function count(...items: unknown[]): number {
return items.length;
}
function forward(...items: unknown[]): number {
return count(items);
}
console.log(count("a", "b", "c"));
console.log(forward("a", "b", "c"));
04Repair the match-report worker
Summaries, retry options, then ordered notices
The worker has three small entry points used by the same report delivery flow. First summarize a player’s submitted scores, including an empty submission. Then resolve each queued job’s attempt option, keeping an explicit opt-out of 0. Finally forward a batch to the existing send helper so the channel receives one notice per message, followed by an accurate sent count.
The shown fixtures include omitted options, explicit zero and positive counts, empty message batches and several messages. These distinguish missing configuration from a deliberate value and a message batch from one list-shaped message.
Repair the match-report worker
The empty call, the difference between left out and zero, then a list passed on as separate arguments.
In the frontend An omitted component prop and an explicit zero carry different retry choices. Optional
The worker’s review panel displays a job’s maximum attempt count. Its component defaults that prop to 3, while a supplied 0 keeps the action disabled. These static examples show the same presence distinction at a component boundary.
type Props = { maxAttempts?: number };
export default function AttemptPreview({ maxAttempts = 3 }: Props) {
return (
<button disabled={maxAttempts === 0}>Plan up to {maxAttempts} attempts</button>
);
}
The destructured prop initializer supplies 3 when maxAttempts is omitted or undefined. An explicit 0 remains 0, so the disabled expression is true. A parent expression such as value || 3 would replace zero before this component receives it. The prop contract here is an optional nonnegative whole count, without null.
Sources: React: default prop values,Svelte: fallback values in $props.
Sign in to keep your read marks on every device. Sign in
Was this helpful?