Skip to content

Generic Callbacks

The Course
•
20 min study

A transform helper has two type relationships: the element the callback receives and the element it returns. mapValues<T, U>(items: readonly T[], transform: (item:T) => U): U[] says each output is the callback's U, not necessarily the input T.

The caller usually supplies no explicit type arguments because array and callback context provide them. A callback can only assume the fields guaranteed by its input contract. A callback promised to handle every Book cannot safely demand an ExtendedBook containing extra required fields.

Under strictFunctionTypes, ordinary function-typed callbacks are checked against these parameter requirements. There are special compatibility rules for method declarations; prefer a function property or function parameter when expressing a callback contract. We do not need to explore variance terminology to use the safe rule: the callback must handle every input the helper promises to pass.

Keep one generic signature here: T names the input element type and U names the output element type. Their names describe a relationship; neither exists as a runtime value.

Predict, run, and explain

Write down what the program prints before you run it. Use Check types first; the program runs only when the real compiler accepts it.

Predict, then runTypeScript
function mapValues<T, U>(items: readonly T[], transform: (item: T) => U): U[] {
  return items.map(transform);
}

const lengths = mapValues(["Types", "API"], text => text.length);

console.log(lengths.join(","));

Enable JavaScript to edit and type-check this example.

Check your prediction
Text
5,3

Read the compiler error

The callback requires more than the caller promises to supply. ExtendedBook below explicitly names both required fields; the plain Book passed by apply has no subtitle.

Find and fix the type errorTypeScript
type Book = { title: string };

type ExtendedBook = {
  title: string;

  subtitle: string;
};

function apply(book: Book, render: (book: Book) => string): string {
  return render(book);
}

apply({ title: "Types" }, (book: ExtendedBook) => book.subtitle);

Enable JavaScript to edit and type-check this example.

Check your diagnosis

Accept Book and handle only its guaranteed fields. If subtitle is required by the operation, change the whole input contract instead of narrowing only the callback parameter.

Infer output from what the callback returns

A reusable transformation has separate input and output element types. T describes what the helper supplies; U describes what the callback produces. Keeping both parameters preserves useful information without forcing every transformation to return its input type.

The callback must accept every input allowed by the helper contract. A function that requires an extra property is unsafe even if one current fixture happens to carry it. Check this relationship at the public boundary rather than relying on an accidental richer record.

U comes from the callbackTypeScript
function mapValues<T, U>(
  values: readonly T[],
  transform: (value: T) => U
): U[] {
  return values.map(transform);
}

const lengths = mapValues(["Types", "API"], value => value.length);

console.log(lengths.join(","));
console.log(lengths.every(value => value >= 0));

Enable JavaScript to edit and type-check this example.

Check your prediction
Text
5,3
true

U is number because the callback returns length. A later push of text should fail checking even though the input happened to be text.

A fixed string callbackTypeScript
type Book = { title: string };

function labels(
  values: readonly Book[],
  render: (book: Book) => string
): string[] {
  return values.map(render);
}

console.log(
  labels([{ title: " Types " }], book => book.title.trim()).join(",")
);
console.log(labels([], book => book.title).length);

Enable JavaScript to edit and type-check this example.

Check your prediction
Text
Types
0

The renderer handles exactly the promised fields. An empty list makes no callback invocations and returns an empty output, which is another part of the transformation contract.

Try the rule on new inputs

Use one input list to produce numbers, booleans and new records in separate calls. Inspect the return types. Then make a callback demand an extra required field and read the parameter-compatibility error.

Your turn

Exercise

Generic Callbacks: practice

Some books have no page count yet (pages?: number). Write keepMap: like mapValues, but the callback may return undefined to mean "skip this item", and the result contains only the values that were kept. Use it to get the known page counts of books A (10), B (no pages) and C (0), printing 10,0, then the titles of books without pages, printing B. The result of the first call must be number[], with no undefined in its type, so pushing text or undefined into it is a compiler error.

Your workspaceTypeScript
// Same two type parameters as mapValues; only the callback's return type changes.

Enable JavaScript to edit and type-check this example.

Hint 1: the idea

If the callback's return type is written as U | undefined, then for book => book.pages the checker matches number | undefined against it and infers U as number, which is exactly the element type you want to return.

Hint 2: a first step

Map alone can't drop items. Loop over the input, call transform once per item, and push the result only when it is not undefined.

Show one solution
One possible solutionTypeScript
function keepMap<T, U>(
  items: readonly T[],
  transform: (item: T) => U | undefined
): U[] {
  const results: U[] = [];
  for (const item of items) {
    const result = transform(item);
    if (result !== undefined) {
      results.push(result);
    }
  }
  return results;
}

type Book = { title: string; pages?: number };

const books: Book[] = [
  { title: "A", pages: 10 },
  { title: "B" },
  { title: "C", pages: 0 }
];

const pages = keepMap(books, book => book.pages);
console.log(pages.join(","));

const untitled = keepMap(books, book =>
  book.pages === undefined ? book.title : undefined
);
console.log(untitled.join(","));

Enable JavaScript to edit and type-check this example.

Text
10,0
B

Putting | undefined in the callback type rather than in U is the key move: U is inferred as number (and as string in the second call), so the return type U[] has no undefined in it and pages.push("x") fails with TS2345. Inside the loop, result !== undefined narrows U | undefined to U, which is what lets push type-check. Using !== undefined rather than a truthiness test is a behavioral choice the types can't make for you: C's 0 pages is kept, as the output shows.

Change it and test it

Try a callback requiring an extra required field not present on every input. Explain why this is unsafe even if one demonstration record has that field. Then compare the direct array.map call with the helper: use abstraction only if it adds useful domain behavior.

Was this article helpful?

Your response is saved on this device.