Come Back Later: Review Challenges

The Course
•
3 min read

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
relationships

Build it yourself

Exercise

Come Back Later: Review Challenges: practise

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.

notes.txt
0

Trace the implementation against the task before comparing styles. A passing example supports that case; use the changes below to challenge the wider contract.

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.

Was this article helpful?

Your response is saved on this device.