Skip to content

Come Back Later: Review Challenges

The Course
•
20 min study

Return after two days, then again after about a week. Hide the solutions and rebuild a derived event union, one checked branded factory and a synchronous channel contract from memory. Explain why a generic key/payload pair can lose correlation when the key is a union.

Next, write one positive consumer and one focused negative consumer. Predict what happens if a listener return changes from undefined to void. Without running code, trace a listener removing another listener from a snapshot and a listener disposing its channel during dispatch.

Finally, solve the settings patch brief with a new setting of your own. Include a runtime validator and a compile-time mismatch case. Explain why adding a key to a type map does not create its decoder or runtime implementation.

Use the hints only after a real attempt. Measure whether you can explain a failed test and repair it, not whether you remember the spelling of every utility type. These delayed review minutes are extra to the seven one-hour study days.

Predict, check, and run

Write the output before running. Use Check types first, then Check & run. Types disappear at runtime: explain both what the compiler checks and what the code actually does. Each editor is an independent ES module; await asynchronous work explicitly.

Predict, then runTypeScript
type KeepText<T> = T extends string ? T : never;

const recalled: KeepText<string | number> = "relationships";

console.log(recalled);

Enable JavaScript to edit and type-check this example.

Check your prediction
Text
relationships

Build it yourself

Exercise

Come Back Later: Review Challenges: practice

Without copying earlier code, derive an event union for {opened:{path:string};closed:{code:number}} and write a narrowed label function. Print notes.txt and 0. Then explain how you would validate an unknown opened event before labeling it.

Your workspaceTypeScript
// Start from the stated contract. Add a valid call and an edge case.

Enable JavaScript to edit and type-check this example.

Hint 1: the idea

A union of paired records keeps more information than two independent unions.

Hint 2: a first step

Map each key to one record before indexing by keyof E.

Show one solution
One possible solutionTypeScript
type Events = {
  opened: { path: string };

  closed: { code: number };
};

type EventOf<E> = {
  [K in keyof E]: {
    type: K;

    payload: E[K];
  };
}[keyof E];

function label(event: EventOf<Events>): string {
  if (event.type === "opened") {
    return event.payload.path;
  }

  return String(event.payload.code);
}

console.log(label({ type: "opened", payload: { path: "notes.txt" } }));
console.log(label({ type: "closed", payload: { code: 0 } }));

Enable JavaScript to edit and type-check this example.

Text
notes.txt
0

The same derived-union pattern as Day 4, with new names: the mapped type pairs each key with its payload, and checking type narrows the payload. To label an unknown opened event, first decode it: check it is an object with type === "opened" and a payload object whose path is a nonblank string, and return a Result. Only an ok result should reach label, so the function never receives unchecked data.

Change it and test it

Rebuild the channel tests before the implementation next time. If you get stuck on a utility type, write the concrete two-branch contract first, then derive it. Review only the prerequisite that blocked your explanation.

Ready for the next step?

Apply the same method to a small library of your own: write its consumer-facing contract, implement runtime checks where input is unknown, and test both accepted and rejected calls. Include a consumer that imports only the public entry point. Then deliberately break one declaration relationship and one runtime behavior, checking that different tests detect them. Keep a short record of decisions you cannot yet explain and retry those after a delay. More complex types are useful only when they make a real consumer's work safer or clearer.

Was this article helpful?

Your response is saved on this device.