Add a remainingMinutes field to the planner report. It should total the minutes of incomplete sessions. A feature touches several places: the Report type declares the new promise, summarize computes it, callers display it, and tests verify it.
Start by changing the type, then follow the diagnostics. A return object missing the new field should fail checking. This is useful guidance about where the contract is incomplete, but it cannot tell you whether your new arithmetic implements the correct rule.
Keep the original completedCount and completedMinutes behavior. Do not rename completedMinutes to totalMinutes while changing its meaning; callers may rely on the original contract. A report can contain both completed and remaining work with clearly named fields.
Test empty input, all-completed input, all-incomplete input and a mixed list. Include zero minutes so your code does not confuse a valid value with absence. If an input is malformed, it belongs in parser tests rather than being patched inside summarize.
This is a small example of a general workflow: state the rule, adjust the type contract, implement the behavior, review compiler diagnostics, test runtime results, and explain the change. You do not need a sophisticated generic abstraction for a three-field report.
Predict, check, then run
Before running, write down the output and explain which information the checker knows. Click Check types first, then Check & run.
const sessions = [
{ minutes: 25, done: true },
{ minutes: 40, done: false }
];
let remaining = 0;
for (const session of sessions)
if (!session.done) {
remaining += session.minutes;
}
console.log(remaining);Enable JavaScript to edit and type-check this example.
Check your prediction
40Add a new fact without changing existing meanings
Remaining minutes is a new report fact, not a new meaning for completedMinutes. Compute a tiny fixture’s expected fields by hand before editing Report. Compiler diagnostics can locate missing construction sites, but cannot prove the arithmetic or ownership policy. Use unequal durations and include zero so a count-based or truthy-default mistake becomes visible.
| Boundary or case | What to inspect |
|---|---|
| Empty | All totals are zero |
| All incomplete | Completed stays zero and remaining sums every duration |
| Mixed | Existing completed behavior survives the new field |
Update the Report type, summarize, its caller and tests as one feature. Re-run the old assertions too. Decide whether derived data belongs in a report or stored state, and name the extra consistency rule if you store it.
Your turn
Extending the Planner: practice
This is the planner's report code, with the first step of the feature already done: remainingMinutes has been added to the Report type. Click Check types and follow the diagnostics to every place that builds a Report. Then make display end with , N min left. Print 1 complete, 25 min, 40 min left for the three sessions and 0 complete, 0 min, 0 min left for an empty list. Keep completedCount and completedMinutes meaning what they meant before.
type Session = { topic: string; minutes: number; done: boolean };
type Report = {
completedCount: number;
completedMinutes: number;
remainingMinutes: number; // new: minutes of unfinished sessions
};
const emptyReport: Report = { completedCount: 0, completedMinutes: 0 };
function summarize(sessions: readonly Session[]): Report {
if (sessions.length === 0) {
return emptyReport;
}
let completedCount = 0;
let completedMinutes = 0;
for (const session of sessions) {
if (session.done) {
completedCount += 1;
completedMinutes += session.minutes;
}
}
return { completedCount, completedMinutes };
}
function display(report: Report): string {
return `${report.completedCount} complete, ${report.completedMinutes} min`;
}
const sessions: Session[] = [
{ topic: "Types", minutes: 25, done: true },
{ topic: "DOM", minutes: 40, done: false },
{ topic: "Review", minutes: 0, done: false }
];
console.log(display(summarize(sessions)));
console.log(display(summarize([])));Enable JavaScript to edit and type-check this example.
Hint 1: the idea
Each TS2741 diagnostic points at an object that does not yet keep the Report's new promise. There are two of them. The third change, in display, produces no diagnostic at all: only your expected output finds it.
Hint 2: a first step
Start with the diagnostic on summarize's return: add a second running total that the loop updates in an else branch.
Show one solution
type Session = { topic: string; minutes: number; done: boolean };
type Report = {
completedCount: number;
completedMinutes: number;
remainingMinutes: number;
};
const emptyReport: Report = {
completedCount: 0,
completedMinutes: 0,
remainingMinutes: 0
};
function summarize(sessions: readonly Session[]): Report {
if (sessions.length === 0) {
return emptyReport;
}
let completedCount = 0;
let completedMinutes = 0;
let remainingMinutes = 0;
for (const session of sessions) {
if (session.done) {
completedCount += 1;
completedMinutes += session.minutes;
} else {
remainingMinutes += session.minutes;
}
}
return { completedCount, completedMinutes, remainingMinutes };
}
function display(report: Report): string {
return `${report.completedCount} complete, ${report.completedMinutes} min, ${report.remainingMinutes} min left`;
}
const sessions: Session[] = [
{ topic: "Types", minutes: 25, done: true },
{ topic: "DOM", minutes: 40, done: false },
{ topic: "Review", minutes: 0, done: false }
];
console.log(display(summarize(sessions)));
console.log(display(summarize([])));Enable JavaScript to edit and type-check this example.
1 complete, 25 min, 40 min left
0 complete, 0 min, 0 min leftChanging the type first turned the feature into a to-do list: TS2741 at emptyReport and TS2741 at summarize's return, one for each place that builds a Report. Neither diagnostic says what the value should be; the else branch is your rule, and the zero-minute Review session checks that it adds rather than counts. display compiled before and after your change because it only reads fields, so the missing "min left" was caught only by comparing the output with the expected line.
Change it and explain it
Add assertions for all-completed and all-incomplete lists. If extending the local capstone, update model.ts, the message in main.ts and tests.mjs. Explain which missing changes the compiler caught and which required a runtime expectation.
Completion is saved in this browser only. It does not measure mastery.