Parameters

Optional params

A question mark after the name makes a parameter optional. Optional parameters hold the value undefined when the caller skips them. Keep required parameters first and optional ones last so calls stay readable.

function greet(name: string, title?: string): string { if (title) { return "Hello, " + title + " " + name; } return "Hello, " + name; } console.log(greet("Ada")); console.log(greet("Ada", "Dr."));

Hello, Ada

Hello, Dr. Ada

Defaults

A default value fills in the argument when the caller passes nothing or passes undefined. Defaults make common cases short while still allowing overrides. TypeScript reads the type from the default, so a separate annotation is rarely needed.

function greet(name: string = "guest"): string { return "Hello, " + name; } const a: string = greet(); const b: string = greet("Ada"); console.log(a); console.log(b);

Hello, guest

Hello, Ada

Rest params

Rest parameters collect any number of trailing arguments into one typed array. The three dots gather the extras, and the array type says what each item must be. Use rest when the argument count is open ended.

function sum(...nums: number[]): number { return nums.reduce((x, y) => x + y, 0); } console.log(sum(1, 2, 3));

6

Destructure params

Destructuring unpacks an object argument straight into named locals. Annotate the whole pattern once with an object type. Callers pass one clear options object instead of a long positional list.

function show({ name, age }: { name: string; age: number }): string { return name + " is " + age; } console.log(show({ name: "Ada", age: 36 }));

Ada is 36

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does a parameter marked with a question mark hold when the caller skips it?

    Answer

    undefined

    An optional parameter is simply not passed, so its value is undefined until the body handles it.

  2. What does a default value such as name: string = "guest" do?

    Answer

    It fills the argument when the caller passes nothing or undefined

    The default covers both an omitted argument and an explicit undefined.

  3. What shape does a rest parameter such as ...nums: number[] collect?

    Answer

    All trailing arguments into one array of numbers

    The three dots gather the remaining arguments, and the array type checks every item.

  4. Where does the annotation go when a function destructures an object parameter?

    Answer

    On the whole pattern, as an object type

    One object type describes the argument, and the destructured names take their types from it.

  5. What does this program print? function greet(name: string, title?: string): string { if (title) { return "Hello, " + title + " " + name; } return "Hello, " + name; } console.log(greet("Ada", "Dr."));

    Answer

    Hello, Dr. Ada

    The title is passed, so the first return puts it before the name.

  6. What does this program print? function greet(name: string = "guest"): string { return "Hello, " + name; } console.log(greet());

    Answer

    Hello, guest

    The call passes nothing, so the default value fills the parameter.

  7. What does this program print? function sum(...nums: number[]): number { return nums.reduce((x, y) => x + y, 0); } console.log(sum(1, 2, 3));

    Answer

    6

    The rest parameter holds three numbers, and reduce adds them from the starting zero.

  8. What does this program print? function show({ name, age }: { name: string; age: number }): string { return name + " is " + age; } console.log(show({ name: "Ada", age: 36 }));

    Answer

    Ada is 36

    Both fields come from the object argument, and the returned text prints.

  9. What does the compiler say about this program? function greet(title?: string, name: string): string { return title + name; } console.log(greet("Ada"));

    Answer

    error TS1016: A required parameter cannot follow an optional parameter.

    The optional parameter must come last, so the order in the header is the error.

  10. What does the compiler say about this program? function greet(name: string, title?: string): string { return title ? title + name : name; } console.log(greet("Ada", "Dr.", "x"));

    Answer

    error TS2554: Expected 1-2 arguments, but got 3.

    The signature allows one or two arguments, and the third one is not part of it.

Article author: Arthur Isaev