Sub topic 1
Running the Compiler and Finding Your Errors
Prerequisites
This chapter assumes you know basic JavaScript and can read TypeScript functions, objects, and type annotations. You should also be able to run npm commands in a terminal.
Running the compiler and finding your errors
The TypeScript compiler checks your source files without running them. Run a project check with:
npx tsc --noEmit --pretty false--noEmit prevents JavaScript output. The compiler only reports type errors.
function double(n: number): number {
return n * 2;
}
const input: string = "21";
double(input);The argument has type string, but double requires a number. The compiler reports this diagnostic:
src/index.ts(6,8): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.What a diagnostic is made of
A diagnostic usually contains:
| Part | Meaning |
|---|---|
| `src/index.ts` | File containing the problem |
| `(6,8)` | Line 6, column 8 |
| `TS2345` | Error code |
| Message | Explanation of the type problem |
Start at the reported location, then inspect the expression and the type expected there.
Using error codes to look things up
Error codes provide a stable search term. Searching for TS2345 plus a short part of the message usually finds the relevant TypeScript documentation or examples. Use the code as a starting point, not as a substitute for reading the full message. The same code can describe different values that are not assignable.
> callout note > Fix the earliest useful diagnostic first. Later errors may disappear when the compiler can infer the correct type again. >
The idea behind most errors: assignability
A value is assignable to a type when it can safely be used where that type is required. A number is assignable to number; a string is not. Conversion at the boundary is usually safer than asserting a type:
const parsed = Number(input);
if (Number.isNaN(parsed)) {
throw new Error(`Not a number: ${input}`);
}
double(parsed);Reading a multi-line error message
Read a chained diagnostic from the outside inward. First identify the failing call or assignment. Then find the nested property or type that differs. For example, if an object fails because mode is string but the target expects "fast" | "safe", the object’s literal was widened. Annotate it with the intended type or use a carefully chosen literal type.
Exercise
Which part of src/index.ts(6,8): error TS2345 tells you where to inspect first?
Show answer
The file and position, src/index.ts(6,8), identify the file, line, and column.