1. Concept library
  2. JavaScript & TypeScript
  3. Module 2: Functions and overloads

Concept

Function types

A function type names the parameters and the return type, such as (a: number) => string. You need one whenever a function is passed around: callbacks, handlers and strategies.

What a session covers

The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.

  1. Write a function type for a callback and use it as a parameter type
  2. Explain what the Function type accepts and why it checks nothing
  3. Type a higher-order function that takes and returns functions

In practice

A few lines that show Function types doing its job.

TypeScript
type Formatter = (value: number) => string;

const money: Formatter = (v) => "$" + v.toFixed(2);
const percent: Formatter = (v) => Math.round(v * 100) + "%";

function render(values: number[], format: Formatter): string[] {
  return values.map(format);
}
render([0.5, 1.25], money);

The common mistake

Watch for

Typing a callback parameter as Function. It accepts anything callable and tells you nothing about arguments or return, so every call through it is unchecked.

What to know first

Your roadmap puts these before Function types, unless you already know them.

How it stays learned

Once a session shows you understand Function types, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.

  1. Day 3First recall, while it is fresh
  2. Day 7Second, a week on
  3. Day 18Last, when it has settled

Where it leads

Topics that build on Function types.