Sub topic 5
Read and Triage the First Compiler Errors
Assume you know modern JavaScript, can run npm commands, and have already created a TypeScript project with tsconfig.json. The compiler is TypeScript's checker. It reads your code and reports places where the types do not agree.
After renaming a file from .js to .ts, do not fix errors in random order. First make the compiler output easy to read:
npx tsc --noEmitThe --noEmit option checks the project without creating JavaScript files. A diagnostic usually includes a file name, line and column, an error code, and a message.
Group errors before fixing them
Read the first few messages and group them by cause. One incorrect type can produce several later errors.
| Error group | Typical message | First response |
|---|---|---|
| Syntax | `';' expected` | Fix the JavaScript or TypeScript syntax |
| Missing type | `Parameter implicitly has an 'any' type` | Add a parameter type or infer one from context |
| Wrong property | `Property 'email' does not exist` | Check the object shape and its spelling |
| Possible absence | `Object is possibly 'undefined'` | Add a check before using the value |
| Module problem | `Cannot find module` | Check the import path and installed declarations |
Start with syntax and module errors. Then fix missing types. Finally handle errors caused by nullable values and incorrect object shapes.
Example: find the root cause
Suppose a converted file searches for a user:
interface User {
name: string;
age: number;
}
function findUser(users: User[], name: string): User | undefined {
return users.find((user) => user.name === name);
}
const user = findUser([{ name: "Ada", age: 36 }], "Ada");
if (user) {
console.log(user.name.toUpperCase());
}The return type includes undefined because find returns no value when it cannot find a match. The if (user) check narrows the value from User | undefined to User. Without that check, using user.name would be unsafe.
When several errors point to one expression, ask these questions:
- What type did TypeScript infer here?
- What type did the code assume?
- Can the value really be missing at runtime?
- Should the function's input or output type change?
- Can a condition narrow the type safely?
Use the error location, not just the message
The marked location is where TypeScript detected the problem. The cause may be earlier. For example, a misspelled property in an object literal can cause an error later when that object is passed to a function.
Fix one small group, then run the compiler again:
npx tsc --noEmitAvoid silencing an unfamiliar error with any or a type assertion. Those choices tell the compiler to trust you, so they can hide the real defect. Understand the value's source first, then choose a precise type or a runtime check.
Exercise
Why should you fix a syntax error before an “object is possibly undefined” error?
Show answer
The syntax error may prevent TypeScript from interpreting later code correctly, so fixing it can remove misleading follow-up diagnostics.