Sub topic 12
Which Error to Fix First
Assume you know basic JavaScript, TypeScript annotations, interfaces, and union types. You should also be able to run npx tsc from a terminal.
A single mistake can produce several diagnostics. For example, an incorrect import can make the compiler report errors in every file that depends on that module. A wrong annotation can make later expressions appear invalid. Fixing errors in the order shown on screen is not always the fastest approach.
Start with the earliest useful cause
Begin with errors that can prevent the compiler from understanding a file:
- Missing modules and invalid imports, such as TS2307.
- Syntax errors.
- Incorrect declarations or missing required properties.
- Type mismatches at assignments and function calls.
- Errors that occur later because of the earlier mistake.
The first diagnostic is useful, but “first” means the earliest cause you can identify, not always the smallest line number. Read nearby diagnostics before changing code.
Separate root errors from consequences
Suppose an import fails:
import { loadUser } from "./user-service";
const user = loadUser();
console.log(user.name);If ./user-service does not exist, the compiler may report TS2307 and then report additional errors because it cannot determine the imported symbol's type. Fix the path, filename, package installation, or module-resolution setting first. Do not add assertions to user while the import is unresolved.
A type mismatch can also create a chain:
type User = {
id: number;
name: string;
};
function greet(user: User): string {
return `Hello, ${user.name}`;
}
const user = { id: "1", name: "Ada" };
greet(user);The important error is at the call. The object has id: string, but User requires id: number. Correct the data at its source or convert it at the input boundary. Do not change User.id to string unless the domain really uses strings.
| Diagnostic pattern | Usually inspect first |
|---|---|
| TS2307: cannot find module | Import path, package, or module settings |
| TS2322 or TS2345: type mismatch | The value's inferred type and the expected type |
| TS2741: missing property | Object construction and required fields |
| TS18048: possibly undefined | The lookup result and its missing-value case |
Use a repeatable loop
Run the compiler without generating JavaScript:
npx tsc --noEmit --pretty falseFor each pass:
- Read the file, line, and column.
- Identify the first error that explains later ones.
- Read the complete message chain.
- Make one focused change.
- Run the compiler again.
Some errors are independent. For example, a missing property in one object and an unresolved import in another file may both need direct fixes. Group diagnostics by root cause rather than assuming every error has one shared cause.
Exercise
An unresolved import is followed by five errors involving the imported value. Which error should you investigate first, and why?
Show answer
Investigate TS2307, the unresolved import, first. Until the compiler can load the module, the later errors may be consequences of missing type information.