Sub topic 2
What a Diagnostic Is Made Of
Assume you know basic JavaScript, TypeScript annotations, interfaces, and union types. You should also be able to run npm or npx commands in a terminal.
A TypeScript diagnostic is a compiler report about code that does not satisfy the rules in your project. A useful diagnostic tells you where the problem is, what kind of problem it is, and why the compiler rejected the code.
Read the location first
Run the compiler without writing JavaScript output:
$ npx tsc --noEmit --pretty false
src/index.ts(6,8): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.The diagnostic has several parts:
| Part | Meaning |
|---|---|
| `src/index.ts` | The file containing the reported problem |
| `(6,8)` | Line 6, column 8 |
| `error` | The severity of the diagnostic |
| `TS2345` | The TypeScript error code |
| Remaining text | The explanation of the problem |
The line and column usually point to the expression that TypeScript cannot accept. Open that location before changing code. The visible underline may cover only one part of a larger mistake.
Use the error code as a label
An error code is a stable search term. TS2345 commonly means that an argument is not assignable to a function parameter. TS2322 commonly means that a value is not assignable to a variable or property. The code identifies the error family, but the message supplies the important types and names.
For example:
function double(n: number): number {
return n * 2;
}
const input: string = "21";
double(input);The compiler reports TS2345 because input has type string, while double requires a number. The error is about assignability: whether one type can safely be used where another type is required.
Follow message chains inward
Some diagnostics contain multiple lines. Start with the outer claim, then read each indented detail until you reach the smallest incompatible value.
type Config = { retries: number; mode: "fast" | "safe" };
function run(config: Config): void {
console.log(config.retries, config.mode);
}
const config = { retries: 3, mode: "fast" };
run(config);The outer message says the argument is not assignable to Config. A nested message identifies mode. The final cause is that the object variable inferred mode as string, but Config permits only the literal types "fast" or "safe".
The compiler widens some values when it infers a variable's type. An annotation preserves the intended type:
const config: Config = { retries: 3, mode: "fast" };
run(config);Separate location from cause
A diagnostic can appear at a function call even when the cause began earlier. In the example above, the call to run is the point where incompatible types meet. The widened config declaration is the better place to fix the problem.
Exercise
In src/app.ts(4,12): error TS2322: Type 'string' is not assignable to type 'number'., what do (4,12), TS2322, and the message each identify?
Show answer
(4,12) identifies line 4 and column 12. TS2322 identifies the assignment error family. The message says a string value was used where a number was required.