A service stores a full profile, but the report should expose only id and displayName. The full profile also has an internal numeric score and a private note. The public function receives trusted Profile data and returns a fresh PublicProfile with exactly the two public keys.
This is projection, not validation of unknown input. Do not silently broaden the task into a universal sanitizer. Input parsing will get its own typed boundary on Day 4. Today the important distinction is that a narrower type annotation does not strip properties.
Test two profiles, compare keys, and verify the original data remains unchanged. Returning a smaller view can be useful for caller APIs, but actual privacy also depends on which real data you transmit; the runtime object matters.
Predict, run, and explain
Write the output and trace the important state changes before checking it. Then run the program. Use Check types first; execution is allowed only when the real compiler accepts the source.
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
Write a small test plan before implementing public profile projection. These cases extend the brief; they are not a second worked solution. Predict the expected behavior independently, then test through the public function. A good ordinary example and a good boundary example should catch different possible mistakes.
| Case to try | What your result should demonstrate |
|---|---|
| Input has a private note | Exclude it from returned runtime keys |
| Normal fields | Retain id and display name exactly as supplied by this trusted-input contract |
| Source inspected afterward | Keep the original note and values intact |
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. If a case requires a concept you have not recalled, revisit the relevant lesson before opening the solution.
Your turn
Checkpoint: Public Records: practise
Define Profile and PublicProfile, then projectProfile. Print p1 Ada, id,displayName and true that the original note remains private in the original record. Assert the returned object has no note key.
// Use an explicit returned object, not a whole-input spread.Enable JavaScript to edit and type-check this example.
Hint 1: the idea
The public contract and the actual key set must agree.
Hint 2: a first step
Build the result from profile.id and profile.displayName only.
Show one solution
type Profile = {
id: string;
displayName: string;
score: number;
note: string;
};
type PublicProfile = Pick<Profile, "id" | "displayName">;
function projectProfile(profile: Profile): PublicProfile {
return { id: profile.id, displayName: profile.displayName };
}
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(result.id, result.displayName);
console.log(Object.keys(result).join(","));
console.log(original.note === "private");Enable JavaScript to edit and type-check this example.
p1 Ada
id,displayName
trueExplain why the implementation meets the stated contract. Which part is enforced by the language or checker, and which part needs behavioral evidence?
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.