Study notes · 10% of the exam

Functions, Overloads and Classes

Know how TypeScript types functions (signatures, overloads, `this`, void callbacks) and classes (modifiers, abstract members, initialisation order and constructor types), and where compile-time checks stop and runtime behaviour begins.

Key points

  1. 1

    Function types need parameter names: (n: number) => string. Call signatures ({ (n: number): string }) also allow properties; construct signatures use new (...) => T.

  2. 2

    A () => void callback type accepts functions that return anything, but a function declared (): void may not return a value.

  3. 3

    Overloads are tried top to bottom and the first match wins. Callers never see the implementation signature, and a union argument needs a signature that accepts the whole union.

  4. 4

    private, protected, readonly and override are compile-time only. #private fields are enforced by the JavaScript runtime and can be brand-checked with #field in obj.

  5. 5

    With ES2022 targets (useDefineForClassFields), derived fields initialise right after super() and before parameter properties are assigned; redeclared fields reset base values unless you use declare.

  6. 6

    implements only checks a class against an interface. It never adds members or contextually types method parameters.

  7. 7

    Use the polymorphic this return type for fluent APIs, and abstract new (...args: any[]) => T when a function must accept abstract classes.

Common traps

  • Writing (string) => number declares a parameter named string of type any.

  • Marking callback parameters optional (index?: number) forces every implementer to handle undefined for no benefit.

  • Calling an overridable method from a base constructor runs the override before the subclass's fields are initialised.

Test yourself on Functions, Overloads and Classes

Ten questions, with the answer and explanation after each one.