Skip to article content
FrontendXLesson 06 / 40

Functions, overloads and this

Go from the first annotated variable to writing your own conditional, mapped and infer-based utility types, while keeping a clear line between compile-time checking and runtime behavior.

intermediate · about 45 minutes · 4 exercises

  • Predict
  • Explain
  • Implement
  • Debug

What you will be able to do

#
  • Type function values, optional, default and rest parameters.
  • Write overload signatures with a single implementation.
  • Type this and understand void return assignability.

Overview

#

Function types

snippet.ts
type Mapper = (value: number) => string;

Optional parameters come after required ones. Defaults infer from the value. Rest parameters collect the tail into an array.

snippet.ts
function join(separator: string, ...parts: string[]): string {
  return parts.join(separator);
}

Overloads

When one function accepts different shapes and returns different types, declare overload signatures above one implementation.

snippet.ts
function parse(input: string): number;
function parse(input: number): string;
function parse(input: string | number): number | string {
  return typeof input === "string" ? Number(input) : String(input);
}

Callers only see the signatures. The implementation signature is not callable directly.

this typing

A leading this parameter is a compile-time annotation and is erased at runtime.

snippet.ts
function label(this: { name: string }): string {
  return this.name;
}

void return values

A function returning a value is assignable to a void-returning type, because the caller promised to ignore the result. This is why callbacks typed () => void accept functions that return something.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Implement

    Write an overloaded `wrap` function: given a string it returns `[string]`, given a number it returns `[number]`, and the implementation returns a one-element array.

    starter.typescript
    export function wrap(value: string): string[];
    export function wrap(value: number): number[];
    export function wrap(value: string | number): Array<string | number> {
      // your code
    }
    

    Reveal the reference answer
    reference.typescript
    export function wrap(value: string): string[];
    export function wrap(value: number): number[];
    export function wrap(value: string | number): Array<string | number> {
      return [value];
    }
    

    Authored expected output: Compiles with no errors. Runtime: wrap("a") is ["a"], wrap(1) is [1].

    The overload signatures give callers precise return types while the implementation handles both.

  2. Exercise 2Predict

    Does this compile? ```ts type Handler = () => void; const h: Handler = () => 42; ```

    Choose the best answer
  3. Exercise 3Debug

    This fails to compile. Fix it so the implementation is valid under the overloads. ```ts function len(value: string): number; function len(value: string[]): number; function len(value: string): number { return value.length; } ```

    starter.typescript
    function len(value: string): number;
    function len(value: string[]): number;
    function len(value: string): number {
      return value.length;
    }
    

    Reveal the reference answer
    reference.typescript
    function len(value: string): number;
    function len(value: string[]): number;
    function len(value: string | string[]): number {
      return value.length;
    }
    

    Authored expected output: Compile error resolved: the implementation signature must be compatible with every overload.

    The implementation parameter must accept the union of all overload parameter types.

  4. Exercise 4Explain

    Why is the implementation signature of an overloaded function not directly callable?

    Reveal the reference answer
    reference.typescript
    The overload signatures define the public contract; the implementation signature only has to be broad enough to handle them. If the implementation signature were callable, a caller could pass a shape that no overload accepts, so TypeScript hides it and resolves calls only against the declared overloads.

    Authored expected output: A written explanation separating public overloads from the implementation.

    This keeps the callable surface precise while allowing a permissive body.

Common misconceptions

#
  • The implementation signature of an overload is callable.
  • A this parameter becomes a real runtime argument.
  • A function returning a value cannot be passed where void is expected.

Follow-up practice

#
  • typescript-generics-basics

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.