Sub topic 12
Which error to fix first
This chapter assumes you can run npx tsc --noEmit and read the file, line, column, code and message of a diagnostic. It also assumes you know interfaces, unions and basic narrowing. Earlier chapters covered those. Here the question is ordering: when the compiler prints twenty errors, which one do you fix first?
Why one mistake produces many errors
The compiler checks every use of a type independently. If a declaration is wrong, each use of it can fail. You then see many diagnostics that share a single cause. Fixing them one by one, top to bottom, is slow and sometimes makes the code worse.
Here is a small example. Someone renamed name to fullName in User but did not update the callers.
interface User {
id: number;
fullName: string;
}
function greet(user: User): string {
return `Hello, ${user.name}`;
}
function initials(user: User): string {
return user.name.charAt(0);
}$ npx tsc --noEmit --pretty false
src/users.ts(7,25): error TS2339: Property 'name' does not exist on type 'User'.
src/users.ts(11,15): error TS2339: Property 'name' does not exist on type 'User'.Both errors point at usages, but the cause is the rename. Before editing either line, ask whether the rename was intended. If it was, update the callers. If it was a mistake, revert the interface and both errors disappear at once. In a real project the same rename could produce dozens of errors across many files.
Signals that errors share a cause
Look for these patterns before you start editing:
- The same message repeats with the same symbol, such as
'name'on'User'. - Many errors mention the same type or function name.
- Errors appear in several files, but all of them involve one exported declaration.
- An error inside a function body mentions a return type you never wrote. The compiler inferred it.
The last pattern matters because inference moves a mistake away from where it was made.
function parsePort(value: string) {
const n = Number(value);
return Number.isNaN(n) ? undefined : n;
}
const port = parsePort("8080");
const next = port + 1;
const label = port.toFixed(0);$ npx tsc --noEmit --pretty false
src/ports.ts(7,14): error TS18048: 'port' is possibly 'undefined'.
src/ports.ts(8,15): error TS18048: 'port' is possibly 'undefined'.This assumes strict is on. The inferred return type of parsePort is number | undefined. Narrowing port at line 7 and again at line 8 would work, but it repeats the same fix. Resolve the value once, where it is created:
function parsePort(value: string) {
const n = Number(value);
return Number.isNaN(n) ? undefined : n;
}
const port = parsePort("8080") ?? 3000;
const next = port + 1;
const label = port.toFixed(0);
console.log(next, label);Now port is number, and both errors are gone. Whether 3000 is the right default is a product decision. If a missing port should stop the program, throw an error instead of supplying a default.
A priority order
The compiler prints diagnostics grouped by file, in the order it checks files. That order is not a ranking of importance. Use this order instead.
| Priority | What to look for | Why it comes first |
|---|---|---|
| 1 | Syntax errors (codes TS1xxx) | A missing brace or parenthesis can make the compiler misread everything after it, so later errors may be noise. |
| 2 | Configuration errors (TS5xxx) and `TS2307` (cannot find module) | Wrong options or unresolved imports change which types the compiler sees. |
| 3 | Errors at declarations: interfaces, type aliases, function signatures, exports | Many usages depend on these. |
| 4 | Errors at call sites and assignments | These are often consequences of priority 3. |
| 5 | Isolated errors with no shared cause | Safe to fix last, in any order. |
Syntax errors come first for a concrete reason. TypeScript parses a file before it checks types. If parsing finds a problem, it recovers by guessing what you meant, and the guesses can produce unrelated type errors further down.
Unresolved imports hide errors
TS2307 is unusual. A module the compiler cannot find is treated as any, so values from it are not type-checked. Fixing the import can therefore make new errors appear, because those values now have real types. This is expected behavior and not a regression. The same effect happens when you install a missing @types package.
Count before you edit
For a large error list, group by error code to see where the volume comes from. This command works in a Unix shell and uses the plain output format:
npx tsc --noEmit --pretty false \
| grep -o "error TS[0-9]*" \
| sort | uniq -c | sort -rnA result like 48 error TS2339 and 3 error TS2322 tells you to look at the TS2339 group first. Then read one of them in full, find the type it refers to, and check whether that type's declaration is wrong. If it is, fix it there.
The loop
Work in short cycles, not in one long editing session.
- Run the compiler and read the first error in each file.
- Pick the error that points at a declaration, an import or the config. Fix it.
- Run the compiler again and compare the count.
- Repeat until only isolated errors remain.
A good fix usually lowers the count by more than one. If your edit removes the error you targeted but the count stays the same or rises, the change probably moved the problem. For example, it may have widened a type until the error appeared at a different place. Stop and reconsider before continuing.
Mistakes to avoid when triaging
- Silencing the usage. Adding
as anyor// @ts-expect-errorto each failing line removes the symptoms and keeps the cause. The next change to the declaration will not warn you. Use these only after you have decided the code is correct and the type is the problem. A later chapter covers when that applies. - Trusting the order of output. An error at line 3 of a file is not more important than one at line 300. A shared type declared at the bottom of a file can cause errors at the top.
- Fixing the type to match one call. If you widen a parameter type to make one caller pass, every other caller now accepts values you may not want. Check how the other callers use the parameter first.
- Ignoring errors in the imported file. When an error appears in file A about a type from file B, the compiler is reporting where the mismatch is observed. The cause may be in B.
- Stopping at the first fix that works. After each fix, confirm that the error count dropped as you expected.
Practice
Exercise
A project prints 31 errors. Counting by code shows 28 of them are TS2339: Property 'email' does not exist on type 'Account', spread across 9 files. What should you check first, and why?
Show answer
Check the declaration of Account. Twenty-eight identical errors on one property of one type almost certainly share one cause: the property was renamed, removed or never added. Fix the interface, or update the property name everywhere if the rename was intended, then rerun the compiler. Editing the 28 usages individually, or casting them to any, would hide the cause.
Exercise
You fix a TS2307: Cannot find module './api' error by correcting the import path. Afterward the compiler reports five new errors in the same file. Did the fix break something?
Show answer
No. An unresolved module is treated as any, so values from it were not checked. Once the import resolves, those values have real types and the compiler can report genuine mismatches. Treat the five errors as newly visible real problems and triage them in the usual order.
Exercise
A file contains a missing closing brace near the top and tsc reports a syntax error (TS1005) plus eleven type errors below it. In what order do you work, and what do you expect after the first fix?
Show answer
Fix the syntax error first and rerun the compiler. Many of the eleven type errors were likely caused by the parser misreading the code after the missing brace, so most should disappear. Triage whatever remains using the priority order: declarations before usages.