Compiler Errors

The Course
•
5 min read

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; Day 6 explains assertions when comparing them with 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.

Predict, then runTypeScript
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
60

Read a real compiler error

The value has the wrong capabilities for this operation. Read which type owns the missing property.

Find and fix the type errorTypeScript
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.

Separate type mistakes from wrong calculations: traceTypeScript
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
120

Both expressions compile. Only multiplication implements the intended conversion from hours to minutes. The expected 120 comes from the problem, not from the checker.

Separate type mistakes from wrong calculations: contrastTypeScript
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
205

The 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

Exercise

Compiler Errors: practise

Write totalMinutes for the supplied numeric inputs. Keep the check clean and print 60. Then explain why the original addition was accepted by the checker.

Your workspaceTypeScript
const sessions = 3;
const minutesPerSession = 20;
// Calculate totalMinutes and print it.

Enable JavaScript to edit and type-check this example.

Hint 1: the idea

Both operands are numbers. Choose the operation required by the problem.

Hint 2: a first step

Three equal blocks of twenty minutes require multiplication.

Show one solution
One possible solutionTypeScript
const sessions = 3;
const minutesPerSession = 20;
const totalMinutes = sessions * minutesPerSession;

console.log(totalMinutes);

Enable JavaScript to edit and type-check this example.

60

Explain the type of each value and which mistake the checker would catch. Compare your approach before copying this answer.

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.

Was this article helpful?

Your response is saved on this device.