Skip to content

Commit 1f5375e

Browse files
authored
Merge pull request #28 from total-typescript/matt/re-tooled-an-early-paragraph-to-focus-on-multi-line-errors
Re-tooled an early paragraph to focus on multi-line errors
2 parents a38e94b + 0096376 commit 1f5375e

File tree

1 file changed

+47
-9
lines changed

1 file changed

+47
-9
lines changed

book-content/chapters/02-ide-superpowers.md

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -173,27 +173,65 @@ Object literal may only specify known properties, but 'artsist' does not exist i
173173

174174
That's because we've said that the `album` variable needs to be of type `Album`, but we've misspelled `artist` as `artsist`. TypeScript is telling us that we've made a mistake, and even suggests the correct spelling.
175175

176-
### Dealing With Far-Away Errors
176+
### Dealing With Multi-Line Errors
177177

178178
However, sometimes TypeScript will give you an error in a more unhelpful spot.
179179

180-
In this example, we have a `FunctionThatReturnsString` type that represents a function that returns a string. An `exampleFunction` that is given that type is defined, but it returns a number instead of a string.
180+
In this example, we have a function called `logUserJobTitle` that logs the job title of a user:
181181

182182
```typescript
183-
type FunctionThatReturnsString = () => string;
183+
const logUserJobTitle = (user: {
184+
job: {
185+
title: string;
186+
};
187+
}) => {
188+
console.log(user.job.title);
189+
};
190+
```
184191

185-
const exampleFunction: FunctionThatReturnsString = () => {
186-
// red squiggly line under `exampleFunction`
192+
Don't worry about the syntax for now - we'll cover it later. The important thing is that `logUserJobTitle` takes a user object with a `job` property that has a `title` property.
187193

188-
// Imagine lots more code here...
194+
Now, let's call `logUserJobTitle` with a user object where the `job.title` is a number, not a string.
189195

190-
return 123;
196+
```typescript
197+
const exampleUser = {
198+
job: {
199+
title: 123,
200+
},
191201
};
202+
203+
logUserJobTitle(exampleUser); // red squiggly line under `exampleUser`
204+
```
205+
206+
It might seem like TypeScript should give us an error on `title` in the `exampleUser` object. But instead, it gives us an error on the `exampleUser` variable itself.
207+
208+
Hovering over `exampleUser` shows the following error message:
209+
192210
```
211+
Argument of type '{ job: { title: number; }; }' is not assignable to parameter of type '{ job: { title: string; }; }'.
212+
The types of 'job.title' are incompatible between these types.
213+
Type 'number' is not assignable to type 'string'.
214+
```
215+
216+
It's multiple lines long, which can feel pretty scary. A good rule of thumb with multi-line errors is to start at the bottom:
217+
218+
```
219+
Type 'number' is not assignable to type 'string'.
220+
```
221+
222+
This tells us that a `number` is being passed into a slot where a `string` is expected. This is the root of the problem.
223+
224+
Let's go to the next line:
225+
226+
```
227+
The types of 'job.title' are incompatible between these types.
228+
```
229+
230+
This tells us that the `title` property in the `job` object is the problem.
193231

194-
It might seem like TypeScript should give us an error on the `return` line, but it gives us an error on the line where we define `exampleFunction` instead.
232+
Already, we understand the issue without needing to see the top line, which is usually a long summary of the problem.
195233

196-
The reason why the error is on the function declaration line instead of the `return` line is because of how TypeScript checks functions. We'll look at this later. But the important thing is that while TypeScript does its best to locate errors, sometimes they might not be where you expect.
234+
Reading errors bottom-to-top can be a helpful strategy when dealing with multi-line TypeScript errors.
197235

198236
## Introspecting Variables and Declarations
199237

0 commit comments

Comments
 (0)