Skip to content

Checkpoint: Regression Evidence

The Course
•
25 min study

Build a small public operation, sortedTitles(books), that takes a readonly list of books with readonly titles and returns their trimmed titles in alphabetical order, without changing the caller's list. Its compile contract rejects a numeric title. Its runtime contract sorts, trims, and leaves the input array in its original order.

Include a positive call, one focused negative caller and behavioral assertions. Then perform two different mutations, each caught by a different layer. Widening title to string | number should break the negative type test. Sorting the caller's array in place (after removing readonly from the parameter so that .sort compiles) should leave the types valid but fail the runtime evidence.

This is the same two-layer strategy used by the final gateway. Twenty-five minutes spent proving a test fails for the right reason is more useful than adding an unused abstraction. Keep all intentional invalid calls out of runtime execution.

Predict, check, and run

Write the output before running. Use Check types first, then Check & run.

Predict, then runTypeScript
const titles = ["Types", "Arrays"];
const sorted = titles.sort();

console.log(sorted.join(","));
console.log(titles.join(","));
console.log(sorted === titles);

Enable JavaScript to edit and type-check this example.

Check your prediction
Text
Arrays,Types
Arrays,Types
true

sort reorders the array it is called on and returns that same array. That is why input preservation is worth testing for a sorting function, unlike a string, which no method can change.

Acceptance cases before you code

Write a small test plan before implementing. Predict the expected behavior independently, then test through the public function.

Case to tryWhat your result should demonstrate
[" Types ", "Arrays"] as titlesReturns Arrays,Types, trimmed and sorted
The same input array afterwardsStill starts with " Types ": the caller's order is unchanged
Numeric-title callerRejected by a focused negative compile test
Sort the input in placeTypes still pass if readonly is removed; only the runtime order check fails

After the core task passes, deliberately break one rule and identify the case that notices it. Restore the implementation and explain the failure in terms of the contract, not the number of assertions.

Build it yourself

Exercise

Checkpoint: Regression Evidence: practice

Write sortedTitles(books: readonly Book[]): string[] where Book has a readonly string title. Add a never-called negative caller passing a numeric title under @ts-expect-error. Assert that [" Types ", "Arrays"] produces Arrays,Types and that the input still starts with " Types "; throw an Error if either check fails. Print the result, then both layers verified.

Your workspaceTypeScript
type Book = { readonly title: string };

// Write sortedTitles, the negative caller and the runtime checks.

Enable JavaScript to edit and type-check this example.

Hint 1: the idea

Sort a new array, never the caller's. The readonly parameter type helps: it removes sort from the input, so the compiler pushes you towards a copy.

Hint 2: a first step

map already returns a new array, so trimming first gives you an array you are allowed to sort. Keep the negative caller's input valid except for the one numeric title.

Show one solution
One possible solutionTypeScript
type Book = { readonly title: string };

function sortedTitles(books: readonly Book[]): string[] {
  return books.map(book => book.title.trim()).sort();
}

function negativeContracts(): void {
  // @ts-expect-error a numeric title is not the public contract
  sortedTitles([{ title: 25 }]);
}

const source: Book[] = [{ title: " Types " }, { title: "Arrays" }];
const result = sortedTitles(source);

if (result.join(",") !== "Arrays,Types") {
  throw new Error("Titles are not trimmed and sorted");
}

if (source[0]?.title !== " Types ") {
  throw new Error("The input array was reordered");
}

console.log(result.join(","));
console.log("both layers verified");

Enable JavaScript to edit and type-check this example.

Text
Arrays,Types
both layers verified

Each layer catches a mutation the other cannot. Widen title to string | number (and use String(book.title) so the body still compiles): the runtime checks still pass, but the negative caller becomes legal and TS2578 "Unused '@ts-expect-error' directive" fails the check. Change the parameter to Book[] and return books.sort(...) with a mapped result: the types are valid and the first check passes, but the input is reordered and "The input array was reordered" is thrown. With the readonly parameter that bug does not compile at all (TS2339 "Property 'sort' does not exist on type 'readonly Book[]'"); the runtime check guards against someone removing it.

Change it and test it

Make both mutations described in the solution, one at a time, and confirm that each fails in the expected layer only. Restore the clean version after each.

Was this article helpful?

Your response is saved on this device.