A diagnostic is a compiler message about your program. Read it in three parts: location, error code, and explanation. TS2322 identifies an assignment mismatch. The line and column point to where the checker detected the conflict, which may be where a value is used rather than where it originated.
Use a small debugging loop: check the program, read the first message, describe what type was expected and what arrived, fix the cause, then check again. One wrong declaration can cause several later messages. Fixing the first cause can remove the rest.
Type errors and runtime errors happen at different stages. A missing property can be caught before execution when the object type is known. A throw statement runs only when execution reaches it. A logic mistake, such as adding when you meant multiplying, can use perfectly valid types and still give the wrong answer. That is why predictions and tests matter.
Our playground intentionally refuses to run a program with compiler errors. Elsewhere, build tools can sometimes emit JavaScript despite errors or strip types without checking them. Do not assume seeing output means a real type check happened.
Avoid using any to silence a message. It turns off useful checking for that value and can spread through the program. A suppression comment asks the checker to ignore a problem; a type assertion asks it to trust a type you supply without checking the data. You do not need either syntax here; The Day 6 JSON lesson shows an assertion letting bad data through, next to real validation. First ask whether the data, the annotation, or the operation is wrong.
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 = 3;
const minutesPerSession = 20;
const incorrectTotal = sessions + minutesPerSession;
console.log(incorrectTotal);
console.log(sessions * minutesPerSession);Enable JavaScript to edit and type-check this example.
Check your prediction
23
60Read a real compiler error
The value has the wrong capabilities for this operation. Read which type owns the missing property.
const minutes = 25;
console.log(minutes.toUpperCase());Enable JavaScript to edit and type-check this example.
Check your diagnosis
TS2339 reports that toUpperCase does not exist on number. If your goal is to display the number, use String(minutes); if your goal is uppercase text, start with an actual string.
Separate type mistakes from wrong calculations
The compiler checks relationships between types, not the meaning of your arithmetic. Two valid numbers can still be combined with the wrong operator or the wrong units. A clean check therefore means the program is eligible to run, not that its answer is correct.
Use a small expected result you can calculate independently. When an error message mentions an assignment or argument, repair that specific relationship first. Then execute a meaningful example; changing types merely to silence a diagnostic can preserve the original behavior bug.
const hours: number = 2;
const minutesPerHour: number = 60;
console.log(hours + minutesPerHour);
console.log(hours * minutesPerHour);Enable JavaScript to edit and type-check this example.
Check your prediction
62
120Both expressions compile. Only multiplication implements the intended conversion from hours to minutes. The expected 120 comes from the problem, not from the checker.
const entered: string = "20";
const converted: number = Number(entered);
console.log(converted + 5);
console.log(entered + 5);Enable JavaScript to edit and type-check this example.
Check your prediction
25
205The annotations document the distinction but do not perform conversion. Number is the runtime operation that creates a numeric value. The original input remains text.
Try the rule on new inputs
Introduce one actual type error and one valid-but-wrong calculation. Predict which Check types catches. For the calculation, choose a concrete expected answer and explain why a runtime test is still necessary.
Your turn
Compiler Errors: practice
This program passes Check types but prints 47. It should print the minutes of study in a 2-hour block after a 15-minute break: 105. Find the valid-but-wrong expression and repair it without changing the three inputs. Then explain why the checker could not report this mistake.
const hours = 2;
const minutesPerHour = 60;
const breakMinutes = 15;
const studyMinutes = hours + minutesPerHour - breakMinutes;
console.log(studyMinutes);Enable JavaScript to edit and type-check this example.
Hint 1: the idea
There is no diagnostic to read: every operand is a number. Compute the right answer by hand first, then find the operator that disagrees with it.
Hint 2: a first step
Turn the hours into minutes first, as in the trace above, and only then subtract the break.
Show one solution
const hours = 2;
const minutesPerHour = 60;
const breakMinutes = 15;
const studyMinutes = hours * minutesPerHour - breakMinutes;
console.log(studyMinutes);Enable JavaScript to edit and type-check this example.
105hours + minutesPerHour is number + number, so the checker had nothing to object to; it gave 62 - 15 = 47. Multiplying converts hours to minutes (120) before the break is subtracted. A type error would have needed a wrong kind of value, such as breakMinutes = "15"; a wrong operator needs an expected answer worked out independently.
Change it and explain it
Change both inputs to zero and predict the output. Then deliberately misspell minutesPerSession and read the compiler message. Restore the spelling. Write one example of a defect types catch and one they do not.
Completion is saved in this browser only. It does not measure mastery.