A service stores a full Profile: id, displayName, a numeric score and a private note. The report shows only some fields, and the team keeps the list of public fields, with their display labels, in one configuration object, publicLabels: id is shown as "ID" and displayName as "Name". This checkpoint combines the whole day:
- satisfies: check publicLabels so that only real Profile keys with string labels are allowed (
displaynamemust be an error), while keeping its exact keys. - structural contracts: derive the PublicProfile type from publicLabels' keys (using
keyof typeof, introduced at the end of the satisfies lesson) rather than writing the field list twice, and write projectProfile so it returns a fresh object with exactly those keys. - modules: imagine Profile, publicLabels and projectProfile live in profile.ts and main.ts uses all three. Write down which of those imports would be
import type, and which line survives in the emitted main.js.
The input is trusted Profile data; validation of unknown input is Day 4's job.
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.
const full = { id: "p1", displayName: "Ada", note: "private" };
const publicView = { id: full.id, displayName: full.displayName };
console.log(JSON.stringify(publicView));Enable JavaScript to edit and type-check this example.
Check your prediction
{"id":"p1","displayName":"Ada"}Acceptance cases before you code
Predict the result of each case before you code. Some are caught by the checker and some only by running.
| Case to try | What your result should demonstrate |
|---|---|
Misspell a key in publicLabels as displayname | Check types rejects the config (satisfies) |
Add score: "Score" to publicLabels | PublicProfile gains score, so projectProfile's return no longer compiles until you project score too |
| Make projectProfile return its input | It still compiles; only the key list and the no-note check fail at runtime |
| Inspect the original afterwards | Its note is unchanged |
The second case is the payoff of deriving the type from the config: changing the configuration turns every out-of-date projection into a diagnostic. The third case is why the runtime key check stays.
Your turn
Checkpoint: Public Records: practice
Implement the brief. Render the projected profile with the labels as ID: p1 | Name: Ada, then print the returned keys (id,displayName) and true that the original note is still "private". Throw if the returned object has a note key. Write your modules answer as a comment.
// Profile, a checked publicLabels config, a derived PublicProfile, projectProfile and render.Enable JavaScript to edit and type-check this example.
Hint 1: the idea
Let the config be the single source of truth. Its key union, read with the keyof typeof operators from the satisfies lesson, is exactly the list of public fields, as long as an annotation has not widened the config's keys to string.
Hint 2: a first step
Start with the check on publicLabels. Ask which keys it may have (only Profile's, and not necessarily all of them) and what type its values have. A beginner utility type expresses "not necessarily all". Then pick the public fields out of Profile using the config's key union.
Show one solution
type Profile = {
id: string;
displayName: string;
score: number;
note: string;
};
const publicLabels = {
id: "ID",
displayName: "Name"
} satisfies Partial<Record<keyof Profile, string>>;
type PublicProfile = Pick<Profile, keyof typeof publicLabels>;
function projectProfile(profile: Profile): PublicProfile {
return { id: profile.id, displayName: profile.displayName };
}
function render(view: PublicProfile): string {
return `${publicLabels.id}: ${view.id} | ${publicLabels.displayName}: ${view.displayName}`;
}
// Modules: Profile and PublicProfile are types, so main.ts would use
// import type { Profile } from "./profile.js";
// import { publicLabels, projectProfile } from "./profile.js";
// and only the second line survives in main.js.
const original: Profile = {
id: "p1",
displayName: "Ada",
score: 10,
note: "private"
};
const result = projectProfile(original);
if ("note" in result) {
throw new Error("Projection leaked note");
}
console.log(render(result));
console.log(Object.keys(result).join(","));
console.log(original.note === "private");Enable JavaScript to edit and type-check this example.
ID: p1 | Name: Ada
id,displayName
truesatisfies rejects a misspelled or non-Profile key (TS2561 suggests displayName for displayname) but keeps the literal keys, so PublicProfile is exactly Pick<Profile, "id" | "displayName">. Adding score to the config would make projectProfile fail with TS2741 until it projects score too. What the checker cannot see is the runtime key set: returning profile itself still satisfies PublicProfile, and only "note" in result and the printed keys catch it. publicLabels is a value used by render, so it needs an ordinary import; Profile is type-only.
Change it and test it
Mutate the function to return the original object and confirm the runtime assertion fails even though checking passes. Restore projection. Explain whether this function would be safe to call with an unvalidated JSON value.
Completion is saved in this browser only. It does not measure mastery.