Sub topic 5
Read and triage the first compiler errors
This chapter assumes you have a working tsconfig.json like the one from the setup chapter (allowJs on, strict off) and have just renamed one .js file to .ts. You should be able to run npx tsc in a terminal and know basic JavaScript. Annotations and interfaces come in the next chapters. Here the goal is to read what the compiler says and decide what to do about each message.
Get a complete error list
Run the compiler without writing output files:
npx tsc --noEmit --pretty false--noEmit checks the project and writes nothing to dist. --pretty false prints one plain line per error, which is easier to search, count and paste into a ticket. The last line of the output tells you how many errors there are. Record that number. It is your baseline for tracking progress.
Here is a small file that was just renamed to src/app.ts. It is plain JavaScript that happens to contain four problems:
const config = {};
config.port = 3000;
function add(a, b) {
return a + b;
}
add(1);
const input = document.getElementById("name");
console.log(input.value);
let count = 0;
count = "many";With strict off, the compiler reports these errors. The output is trimmed to line, code and message:
src/app.ts, line 2: error TS2339: Property 'port' does not exist on type '{}'.
src/app.ts, line 8: error TS2554: Expected 2 arguments, but got 1.
src/app.ts, line 11: error TS2339: Property 'value' does not exist on type 'HTMLElement'.
src/app.ts, line 14: error TS2322: Type 'string' is not assignable to type 'number'.Anatomy of an error
Each error has three parts:
- Location. The file, line and column where the compiler lost the thread. The real cause is sometimes earlier. An error on line 11 can come from how a variable was created on line 10.
- Code.
TS2339is stable across versions and machines. Search for the code plus a few words of the message, not the whole message with your variable names in it. - Message. Often followed by indented lines that explain why. Read those lines. The first line says what failed, and the indented lines say where the mismatch is.
For example, passing an object with a missing field produces a nested message:
error TS2345: Argument of type '{ name: string; }' is not assignable to parameter of type 'User'.
Property 'age' is missing in type '{ name: string; }' but required in type 'User'.The second line tells you the fix: add age, or make age optional if that matches how the code is really used.
The four kinds of error
Most first-pass errors fall into one of four groups. The right response differs for each, so sort before you edit.
| Kind | Typical codes | What it means | Response |
|---|---|---|---|
| Environment | TS2304, TS2307, TS2580 | The compiler cannot find a name, module or global such as `require` or `process`. | Fix configuration or install declarations. No code change. |
| Missing description | TS2339, TS7006, TS7053 | The code is fine at runtime, but the compiler has no type information to confirm it. | Add a type: annotation, interface, narrowing. |
| Real bug | TS2554, TS2322, TS2345 | The code does something that is likely wrong at runtime. | Fix the code. Treat it as a find. |
| Cascade | Any | An earlier error left a value typed wrong, and later lines fail because of it. | Fix the earlier error and recompile. |
In the example, line 2 is a missing description. The compiler infers {} for config, so it does not know about port. Line 8 is a real bug, because add needs two arguments and b would be undefined. Line 11 is a missing description. The compiler knows getElementById returns HTMLElement | null, and plain HTMLElement has no value. Line 14 is probably a real bug, or a variable that is reused for two meanings.
Triage order
Work in this order. It keeps you from fixing the same problem twice.
- Environment errors first. A missing
requiretype causes a long list of follow-on errors in code that is fine. Check that@types/nodeis installed and thatincludeintsconfig.jsoncovers the file. - Top-to-bottom within the file. Cascades flow downward, so the earliest error is the most likely cause of later ones. Fix one or two, recompile, and see how many vanish.
- Real bugs next. Do not paper over them with a type that makes the error disappear. Fix
add(1)by correcting the call. Do not changebto be optional just to quiet the compiler. - Missing descriptions last. These are the bulk of the work, and the following chapters cover the tools for them.
Fixing the example
Here is the same file with each error addressed according to its kind:
interface Config {
port?: number;
}
const config: Config = {};
config.port = 3000;
function add(a: number, b: number): number {
return a + b;
}
add(1, 2);
const input = document.getElementById("name");
if (input instanceof HTMLInputElement) {
console.log(input.value);
}
let count = 0;
count = 10;The Config interface describes the object, add receives its second argument, and the instanceof check narrows input from HTMLElement | null to HTMLInputElement. The count line was a bug: the intent was clearly a number. The next chapters explain each of these tools in detail.
One more gotcha: files without imports
A file with no import or export is a script, not a module. The compiler treats its top-level names as global. If two such files both declare const config, you get TS2451: Cannot redeclare block-scoped variable 'config', even though each file ran in isolation as JavaScript. Adding export {}; at the end of the file makes it a module and gives it its own scope.
Silencing an error on purpose
Sometimes you cannot fix an error yet. Use a targeted comment with a reason:
// TODO(migration): legacy API returns a different shape. Remove when typed.
// @ts-expect-error
legacyClient.send(payload, true);Prefer @ts-expect-error to @ts-ignore. Once the underlying problem is fixed, the compiler reports the comment itself as an error ("Unused '@ts-expect-error' directive"), so stale suppressions get cleaned up. @ts-ignore stays silent forever. Avoid // @ts-nocheck on a converted file. It turns the file back into untyped JavaScript and defeats the point of renaming it.
Common mistakes
- Fixing errors bottom-up. You waste effort on cascades that disappear after an earlier fix.
- Adding
anyto make red lines go away. It hides the problem and removes checking from every value that touches it. Later chapters cover better options. - Fixing the type when the code is wrong. If
add(1)is a mistake, the fix belongs at the call site. - Ignoring the indented lines. The explanation of a mismatch is usually in the second or third line of the message.
- Searching with local names. Search for the error code and the generic part of the message.
Exercise
After renaming a file to .ts, the compiler reports TS2580: Cannot find name 'require' on line 1, followed by TS2339 errors on lines 5, 9 and 12 that all involve the value returned by require. In what order do you handle them, and what is the likely fix?
Show answer
Handle the TS2580 first, because it is an environment error and the TS2339 errors on lines 5, 9 and 12 are probably cascades from it. The likely fix is to install Node's type declarations with npm install --save-dev @types/node, and to check that tsconfig.json includes the file. Then recompile and see which TS2339 errors remain. Only the ones that survive are real missing descriptions.