You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: book-content/chapters/02-ide-superpowers.md
+47-9Lines changed: 47 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -173,27 +173,65 @@ Object literal may only specify known properties, but 'artsist' does not exist i
173
173
174
174
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.
175
175
176
-
### Dealing With Far-Away Errors
176
+
### Dealing With Multi-Line Errors
177
177
178
178
However, sometimes TypeScript will give you an error in a more unhelpful spot.
179
179
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:
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.
187
193
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.
189
195
190
-
return123;
196
+
```typescript
197
+
const exampleUser = {
198
+
job: {
199
+
title: 123,
200
+
},
191
201
};
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
+
192
210
```
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.
193
231
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.
195
233
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.
0 commit comments