Sub topic 5
Reading a multi-line error message
This chapter assumes you can run tsc, know what a diagnostic's file, position, and code mean (covered earlier), and know that assignability is the question behind most errors. It teaches you to read the indented lines under the first line of a diagnostic, because those lines name the cause.
Why the first line is not enough
The first line of a diagnostic states the outermost claim: this value is not assignable to that type. When both types are simple, such as string and number, that claim is the whole story. When they are objects, unions, or function types, the compiler cannot say in one line which part is wrong. It prints the claim, then adds indented lines that narrow down the cause, one level per line.
Take this program:
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);Running npx tsc --noEmit --pretty false reports:
src/index.ts(8,5): error TS2345: 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"'.The --pretty false flag keeps the output as plain text, so the indentation is easy to see. With the default pretty output, the same lines appear with colors and a source excerpt.
The structure of a chain
Each indented line explains the line above it. Read it as a series of "because" statements:
- The call
run(config)fails because{ retries: number; mode: string }is not assignable toConfig. - That is because the types of property
modeare incompatible. - That is because
stringis not assignable to"fast" | "safe".
The first line is the outermost claim and the last line is the innermost cause. Only the first line carries the file position and the error code. The indented lines have no code of their own, so when you search for the error, use the code and the first line, and read the rest to understand it.
Reading the first line carefully
The first line contains two types, and each one tells you something:
- The source type, after
Argument of type, is what the compiler inferred for your value. In the example it is{ retries: number; mode: string; }. This is the compiler's view of your value, and it can differ from what you meant. You wrote"fast", but the compiler recordedstring. - The target type, after
parameter of type, is what the receiving side demands. Here it isConfig.
Comparing the two printed types property by property often shows the problem before you read the indented lines. In this case mode is string on one side and a union of literals on the other.
Finding the real cause
The innermost line says what is incompatible. It does not say where to fix it. The error is reported at config in run(config), but the cause is on the line where config was declared. The object literal { retries: 3, mode: "fast" } was inferred with mode: string, because a property of a mutable object is widened from the literal "fast" to string. By the time the call is checked, the information that the value was "fast" has been lost.
So the fix goes at the source of the widening. Annotate the variable with the type you intended:
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);Now the literal "fast" is checked directly against "fast" | "safe" when the variable is initialized, and the call at the end passes.
A deeper chain
Chains grow by one line for each level of nesting. Here an object is nested inside another:
interface Address {
city: string;
zip: number;
}
interface Customer {
name: string;
address: Address;
}
const address = { city: "London", zip: "N1" };
const customer: Customer = { name: "Ada", address };The compiler reports:
src/customer.ts(12,43): error TS2322: Type '{ city: string; zip: string; }' is not assignable to type 'Customer'.Wait: this is the case where the claim concerns the whole object, and the compiler descends through the nesting. In practice it reports the claim at the property where the mismatch begins and then walks inward:
src/customer.ts(12,43): error TS2322: Type '{ city: string; zip: string; }' is not assignable to type 'Address'.
Types of property 'zip' are incompatible.
Type 'string' is not assignable to type 'number'.The error position (12,43) points at the shorthand property address. That is the place where Address was expected, so the first line compares the type of address with Address, not customer with Customer. The compiler skips the levels that match and names the first one that does not.
This matters when you read a chain. Do not assume the first line mentions the type you were thinking of. Start from the types the line actually prints.
There are two valid fixes here, and the chain helps you choose between them. If the data is wrong, because a zip code should be a number, change "N1". If the type is wrong, because UK postcodes are not numbers, change zip: number to zip: string in Address. The chain tells you what is incompatible, and you decide which side is correct.
Common shapes of chains
Some phrases recur in message chains. Knowing them speeds up reading.
| Line in the chain | Meaning |
|---|---|
| `Types of property 'x' are incompatible.` | The property `x` exists on both types, but its types differ. The next line gives the difference. |
| `Property 'x' is missing in type 'A' but required in type 'B'.` | The source has no `x`, and the target requires it. |
| `Type 'A' is not assignable to type 'B'.` at the deepest level | The innermost comparison that failed. This is usually the cause. |
| `Type 'X' is not assignable to type 'Y'.` followed by `Type 'X' is not assignable to type 'Z'.` | A union target: the value was checked against each member and matched none. |
| `Types of parameters 'a' and 'b' are incompatible.` | Two function types disagree about a parameter. |
When the target is a union, the compiler may print several lines at the same depth, one per member it tried. This looks longer than it is. You only need to understand why the value matches none of the options, and the most relevant member is usually the one whose shape is closest to your value.
A reading procedure
When you meet a multi-line diagnostic, work through these steps:
- Note the position and code from the first line.
- Read the two whole types on the first line and compare them yourself.
- Follow each indented line down to the last one, and say each step as a "because".
- Identify the innermost mismatch.
- Trace where the mismatched type originated, then choose a fix: change the value, the declared type, or the annotation that caused widening.
Exercise
This code fails to compile. The compiler reports: Type '{ id: number; tags: string[]; }' is not assignable to type 'Item'. Types of property 'tags' are incompatible. Type 'string[]' is not assignable to type '[string, string]'. Which line of the message names the innermost cause, what is the cause, and where would you fix it? type Item = { id: number; tags: [string, string] }; const tags = ["a", "b"]; const item: Item = { id: 1, tags };
Show answer
The last line is the innermost cause: a string[] (array of any length) is not assignable to the tuple [string, string], which requires exactly two elements. The error is reported at the use in the Item object, but the cause is the declaration of tags, where the array literal was inferred as string[]. Fix it at the source by annotating the variable, for example const tags: [string, string] = ["a", "b"];, or by changing Item.tags to string[] if the length does not matter.