Sub topic 5
Reading a Multi-line TypeScript Error Message
Assume you know basic JavaScript, TypeScript annotations, interfaces, and union types. You should also be able to run npx tsc from a terminal. This chapter focuses on reading a diagnostic whose message contains several levels of detail.
Start with the outermost claim
TypeScript usually reports the error at the place where your code violates a type rule. The first line gives the main claim. The following lines explain why the claim is true.
Consider this example:
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 compiler may report:
Argument of type '{ retries: number; mode: string; }' is not assignable to parameter of type 'Config'.
Types of property 'mode' are incompatible.
Type 'string' is not assignable to type '"fast" | "safe"'.Read this from the outside inward:
- The call passes an argument to
run. - The argument's type is not assignable to
Config. - The
modeproperties are the part that differs. - The argument has
mode: string, butConfigrequires the narrower union"fast" | "safe".
The final line is the root cause. The first line identifies the larger operation that failed.
Understand the nested types
A type is assignable when a value can safely be used where another type is expected. A general string might contain "fast", but it might also contain "slow". Therefore, TypeScript cannot safely use every string as "fast" | "safe".
The variable annotation fixes the type at the point where the object is created:
type Config = {
retries: number;
mode: "fast" | "safe";
};
function run(config: Config): void {
console.log(config.retries, config.mode);
}
const config: Config = { retries: 3, mode: "fast" };
run(config);The annotation prevents the string literal from widening to the broader string type.
Use the location and code with the message
A complete diagnostic might look like this:
src/config.ts(12,5): error TS2345: Argument of type ...The parts identify:
src/config.ts: the file(12,5): line 12, column 5TS2345: the diagnostic code- The remaining text: the explanation
The code helps you search TypeScript documentation or your editor's details. The message explains the specific instance in your program.
A practical reading method
When you see a multi-line diagnostic:
- Go to the reported location.
- Identify the operation, such as an assignment or function call.
- Read the first message line to find the expected and actual types.
- Follow each indented line to the differing property or type.
- Fix the narrowest cause you can explain.
- Run the compiler again, because later diagnostics may depend on the first error.
Exercise
Why is a variable with type { mode: string } not assignable to { mode: "fast" | "safe" }?
Show answer
The wider string type may contain values such as "slow", so it does not guarantee that mode is one of the two allowed literals.