Function types

Annotate signatures

A signature is the list of parameter types plus the return type, and every TypeScript function header can carry one. Writing them down turns the function header into documentation that the compiler checks on every call. Start here whenever a function accepts input or produces output.

function add(a: number, b: number): number { return a + b; } const total: number = add(2, 3); console.log(total);

5

Type the whole function

Sometimes the variable needs the type, not the function itself. A function type describes the full shape with an arrow between the parameter list and the return type. Assign any matching function and the variable keeps the same contract.

type Greet = (name: string) => string; const greet: Greet = (name) => { return "Hello, " + name; }; console.log(greet("Ada"));

Hello, Ada

void returns

Use void when the function returns nothing useful and only performs an action. Logging, saving, and alerting are classic void functions. Callers know there is no value to read, so they never use the result by mistake.

function log(message: string): void { console.log(message); } log("Done");

Done

never returns

Use never when the function cannot return at all because it always throws or loops forever. A helper that raises errors is the most common case. After calling it, the compiler knows the code below is unreachable.

function fail(message: string): never { throw new Error(message); }

Questions

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

  1. What does the signature of a function name?

    Answer

    The parameter types and the return type

    The signature is the header contract, and every call is checked against it.

  2. What does a function type such as (name: string) => string describe?

    Answer

    A variable that can hold any function with that shape

    The arrow separates the parameters from the return type, so the variable accepts any matching function.

  3. When do you give a function the return type void?

    Answer

    When it performs an action and returns no useful value

    void tells callers that there is no result to read, which fits logging and saving.

  4. When do you give a function the return type never?

    Answer

    When it cannot return at all, because it always throws or never ends

    never promises that no call returns normally, which is how the compiler proves code below is unreachable.

  5. What does this program print? function add(a: number, b: number): number { return a + b; } const total: number = add(2, 3); console.log(total);

    Answer

    5

    The function adds the two numbers, and the printed constant holds the result.

  6. What does this program print? type Greet = (name: string) => string; const greet: Greet = (name) => { return "Hello, " + name; }; console.log(greet("Ada"));

    Answer

    Hello, Ada

    The parameter type comes from the function type, and the returned text is printed.

  7. What does this program print? function log(message: string): void { console.log(message); } log("Done");

    Answer

    Done

    The function has no return value, but it still prints its argument.

  8. What does the compiler say about this program? function add(a: number, b: number): string { return a + b; } console.log(add(2, 3));

    Answer

    error TS2322: Type 'number' is not assignable to type 'string'.

    The declared return type promises text, and the sum is a number.

  9. What does the compiler say about this program? function add(a: number, b: number): number { return a + b; } console.log(add("2", 3));

    Answer

    error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.

    The parameter type is number, so the string argument stops the build at the call.

  10. What does the compiler say about this program? function fail(): never { return "no"; } console.log(fail());

    Answer

    error TS2322: Type '"no"' is not assignable to type 'never'.

    A never function cannot return a value, and the returned literal breaks that promise.

Article author: Arthur Isaev