@@ -283,10 +283,7 @@ Finally, we can see that our `test` result has an error because the `result` is
283
283
284
284
Your challenge is to consider how we can change the types to make the errors go away, and to ensure that ` result ` is a ` number ` . You can hover over ` result ` to check it.
285
285
286
- <Exercise
287
- title="Exercise 1: Basic Types with Function Parameters"
288
- filePath="/src/015-essential-types-and-annotations/020-basic-types-with-function-parameters.problem.ts"
289
- />
286
+ <Exercise title =" Exercise 1: Basic Types with Function Parameters " filePath =" /src/015-essential-types-and-annotations/020-basic-types-with-function-parameters.problem.ts " ></Exercise >
290
287
291
288
#### Exercise 2: Annotating Empty Parameters
292
289
@@ -320,10 +317,7 @@ type test = Expect<Equal<typeof result, string>>;
320
317
321
318
Your job is to add some function paramater annotations to the ` concatTwoStrings ` function to make the errors go away.
322
319
323
- <Exercise
324
- title="Exercise 2: Annotating Empty Parameters"
325
- filePath="/src/015-essential-types-and-annotations/021-annotating-empty-parameters.problem.ts"
326
- />
320
+ <Exercise title =" Exercise 2: Annotating Empty Parameters " filePath =" /src/015-essential-types-and-annotations/021-annotating-empty-parameters.problem.ts " ></Exercise >
327
321
328
322
#### Exercise 3: The Basic Types
329
323
@@ -360,10 +354,7 @@ Type 'boolean' is not assignable to type 'string'.
360
354
361
355
Change the types of the annotations on each variable to make the errors go away.
362
356
363
- <Exercise
364
- title="Exercise 3: The Basic Types"
365
- filePath="/src/015-essential-types-and-annotations/022-all-types.problem.ts"
366
- />
357
+ <Exercise title =" Exercise 3: The Basic Types " filePath =" /src/015-essential-types-and-annotations/022-all-types.problem.ts " ></Exercise >
367
358
368
359
#### Exercise 4: The ` any ` Type
369
360
@@ -388,7 +379,7 @@ it("Should handle a form submit", () => {
388
379
const form = document .createElement (" form" );
389
380
390
381
form .innerHTML = `
391
- <input name="name" value="John Doe" / >
382
+ <input name="name" value="John Doe"></Exercise >
392
383
` ;
393
384
394
385
form .onsubmit = (e ) => {
@@ -423,10 +414,7 @@ Why is this error happening? Why isn't TypeScript giving us an error here?
423
414
424
415
I'll give you a clue. I've hidden a nasty typo in there. Can you fix it?
425
416
426
- <Exercise
427
- title="Exercise 4: The ` any ` Type"
428
- filePath="/src/015-essential-types-and-annotations/032.5-any.problem.ts"
429
- />
417
+ <Exercise title =" Exercise 4: The `any` Type " filePath =" /src/015-essential-types-and-annotations/032.5-any.problem.ts " ></Exercise >
430
418
431
419
#### Solution 1: Basic Types with Function Parameters
432
420
@@ -616,10 +604,7 @@ We can tell from the `concatName` function body that it expects `user.first` and
616
604
617
605
How could we type the ` user ` parameter to ensure that it has these properties and that they are of the correct type?
618
606
619
- <Exercise
620
- title="Exercise 1: Object Literal Types"
621
- filePath="/src/015-essential-types-and-annotations/025-object-literal-types.problem.ts"
622
- />
607
+ <Exercise title =" Exercise 1: Object Literal Types " filePath =" /src/015-essential-types-and-annotations/025-object-literal-types.problem.ts " ></Exercise >
623
608
624
609
#### Exercise 2: Optional Property Types
625
610
@@ -668,10 +653,7 @@ The error tells us that we are missing a property, but the error is incorrect. W
668
653
669
654
How would you update this function to fix the errors?
670
655
671
- <Exercise
672
- title="Exercise 2: Optional Property Types"
673
- filePath="/src/015-essential-types-and-annotations/026-optional-property-types.problem.ts"
674
- />
656
+ <Exercise title =" Exercise 2: Optional Property Types " filePath =" /src/015-essential-types-and-annotations/026-optional-property-types.problem.ts " ></Exercise >
675
657
676
658
#### Solution 1: Object Literal Types
677
659
@@ -857,10 +839,7 @@ Even though everything is working as expected, there's an opportunity for refact
857
839
858
840
How could you use the ` type ` keyword to make this code more readable?
859
841
860
- <Exercise
861
- title="Exercise 1: The ` type ` Keyword"
862
- filePath="/src/015-essential-types-and-annotations/027-type-keyword.problem.ts"
863
- />
842
+ <Exercise title =" Exercise 1: The `type` Keyword " filePath =" /src/015-essential-types-and-annotations/027-type-keyword.problem.ts " ></Exercise >
864
843
865
844
#### Solution 1: The ` type ` Keyword
866
845
@@ -1072,10 +1051,7 @@ As the error message points out, there is not currently a property called `items
1072
1051
1073
1052
How would you fix this error?
1074
1053
1075
- <Exercise
1076
- title="Exercise 1: Array Type"
1077
- filePath="/src/015-essential-types-and-annotations/028-arrays.problem.ts"
1078
- />
1054
+ <Exercise title =" Exercise 1: Array Type " filePath =" /src/015-essential-types-and-annotations/028-arrays.problem.ts " ></Exercise >
1079
1055
1080
1056
#### Exercise 2: Arrays of Objects
1081
1057
@@ -1112,10 +1088,7 @@ Object literal may only specify known properties, and 'ingredients' does not exi
1112
1088
1113
1089
By combining what you've seen with typing object properties and working with arrays, how would you specify ingredients for the ` Recipe ` type?
1114
1090
1115
- <Exercise
1116
- title="Exercise 2: Arrays of Objects"
1117
- filePath="/src/015-essential-types-and-annotations/029-arrays-of-objects.problem.ts"
1118
- />
1091
+ <Exercise title =" Exercise 2: Arrays of Objects " filePath =" /src/015-essential-types-and-annotations/029-arrays-of-objects.problem.ts " ></Exercise >
1119
1092
1120
1093
#### Exercise 3: Tuples
1121
1094
@@ -1170,10 +1143,7 @@ setRange([0, 10, 20]);
1170
1143
1171
1144
The code for the ` setRange ` function needs an updated type annotation to specify that it only accepts a tuple of two numbers.
1172
1145
1173
- <Exercise
1174
- title="Exercise 3: Tuples"
1175
- filePath="/src/015-essential-types-and-annotations/031-tuples.problem.ts"
1176
- />
1146
+ <Exercise title =" Exercise 3: Tuples " filePath =" /src/015-essential-types-and-annotations/031-tuples.problem.ts " ></Exercise >
1177
1147
1178
1148
#### Exercise 4: Optional Members of Tuples
1179
1149
@@ -1201,10 +1171,7 @@ const goToLocation = (coordinates: Array<number>) => {
1201
1171
1202
1172
Your challenge is to update the type annotation for the ` coordinates ` parameter to specify that it should be a tuple of three numbers, where the third number is optional.
1203
1173
1204
- <Exercise
1205
- title="Exercise 4: Optional Members of Tuples"
1206
- filePath="/src/015-essential-types-and-annotations/032-optional-members-of-tuples.problem.ts"
1207
- />
1174
+ <Exercise title =" Exercise 4: Optional Members of Tuples " filePath =" /src/015-essential-types-and-annotations/032-optional-members-of-tuples.problem.ts " ></Exercise >
1208
1175
1209
1176
#### Solution 1: Array Type
1210
1177
@@ -1486,10 +1453,7 @@ new () => Map<any, any> (+3 overloads)
1486
1453
1487
1454
How would we type the ` userMap ` so the key must be a number and the value is an object with ` name ` and ` age ` properties?
1488
1455
1489
- <Exercise
1490
- title="Exercise 1: Passing Types to Map"
1491
- filePath="/src/015-essential-types-and-annotations/036-pass-types-to-map.problem.ts"
1492
- />
1456
+ <Exercise title =" Exercise 1: Passing Types to Map " filePath =" /src/015-essential-types-and-annotations/036-pass-types-to-map.problem.ts " ></Exercise >
1493
1457
1494
1458
#### Exercise 2: ` JSON.parse() ` Can't Receive Type Arguments
1495
1459
@@ -1543,10 +1507,7 @@ The test errors tell us that the type of `parsed` is not what we expect. The pro
1543
1507
1544
1508
Why this is happening? What would be an different way to correct these type errors?
1545
1509
1546
- <Exercise
1547
- title="Exercise 2: ` JSON.parse() ` Can't Receive Type Arguments"
1548
- filePath="/src/015-essential-types-and-annotations/037-json-parse-cant-receive-type-arguments.problem.ts"
1549
- />
1510
+ <Exercise title =" Exercise 2: `JSON.parse()` Can't Receive Type Arguments " filePath =" /src/015-essential-types-and-annotations/037-json-parse-cant-receive-type-arguments.problem.ts " ></Exercise >
1550
1511
1551
1512
#### Solution 1: Passing Types to Map
1552
1513
@@ -1911,10 +1872,7 @@ const result2 = concatName("John");
1911
1872
1912
1873
Try to use an optional parameter annotation to fix the error.
1913
1874
1914
- <Exercise
1915
- title="Exercise 1: Optional Function Parameters"
1916
- filePath="/src/015-essential-types-and-annotations/023-optional-function-parameters.problem.ts"
1917
- />
1875
+ <Exercise title =" Exercise 1: Optional Function Parameters " filePath =" /src/015-essential-types-and-annotations/023-optional-function-parameters.problem.ts " ></Exercise >
1918
1876
1919
1877
#### Exercise 2: Default Function Parameters
1920
1878
@@ -1972,10 +1930,7 @@ expect(result).toEqual("John Pocock");
1972
1930
1973
1931
Update the ` concatName ` function to use ` Pocock ` as the default last name if one is not provided.
1974
1932
1975
- <Exercise
1976
- title="Exercise 2: Default Function Parameters"
1977
- filePath="/src/015-essential-types-and-annotations/024-default-function-parameters.problem.ts"
1978
- />
1933
+ <Exercise title =" Exercise 2: Default Function Parameters " filePath =" /src/015-essential-types-and-annotations/024-default-function-parameters.problem.ts " ></Exercise >
1979
1934
1980
1935
#### Exercise 3: Rest Parameters
1981
1936
@@ -2004,10 +1959,7 @@ The test passes, but there's an error on the `...strings` rest parameter.
2004
1959
2005
1960
How would you update the rest parameter to specify that it should be an array of strings?
2006
1961
2007
- <Exercise
2008
- title="Exercise 3: Rest Parameters"
2009
- filePath="/src/015-essential-types-and-annotations/030-rest-parameters.problem.ts"
2010
- />
1962
+ <Exercise title =" Exercise 3: Rest Parameters " filePath =" /src/015-essential-types-and-annotations/030-rest-parameters.problem.ts " ></Exercise >
2011
1963
2012
1964
#### Exercise 4: Function Types
2013
1965
@@ -2081,10 +2033,7 @@ modifyUser(
2081
2033
2082
2034
How would you type ` makeChange ` as a function takes in a ` User ` and returns a ` User ` ?
2083
2035
2084
- <Exercise
2085
- title="Exercise 4: Function Types"
2086
- filePath="/src/015-essential-types-and-annotations/033-function-types.problem.ts"
2087
- />
2036
+ <Exercise title =" Exercise 4: Function Types " filePath =" /src/015-essential-types-and-annotations/033-function-types.problem.ts " ></Exercise >
2088
2037
2089
2038
#### Exercise 5: Functions Returning ` void `
2090
2039
@@ -2125,10 +2074,7 @@ This is triggering our `@ts-expect-error` directive.
2125
2074
2126
2075
How should ` addClickEventListener ` be typed so that each error is resolved?
2127
2076
2128
- <Exercise
2129
- title="Exercise 5: Functions Returning ` void ` "
2130
- filePath="/src/015-essential-types-and-annotations/034-functions-returning-void.problem.ts"
2131
- />
2077
+ <Exercise title =" Exercise 5: Functions Returning `void` " filePath =" /src/015-essential-types-and-annotations/034-functions-returning-void.problem.ts " ></Exercise >
2132
2078
2133
2079
#### Exercise 6: ` void ` vs ` undefined `
2134
2080
@@ -2158,10 +2104,7 @@ acceptsCallback(returnString);
2158
2104
2159
2105
Why is this happening? Can we alter the type of ` acceptsCallback ` to fix this error?
2160
2106
2161
- <Exercise
2162
- title="Exercise 6: ` void ` vs ` undefined ` "
2163
- filePath="/src/015-essential-types-and-annotations/034.5-void-vs-undefined.problem.ts"
2164
- />
2107
+ <Exercise title =" Exercise 6: `void` vs `undefined` " filePath =" /src/015-essential-types-and-annotations/034.5-void-vs-undefined.problem.ts " ></Exercise >
2165
2108
2166
2109
#### Exercise 7: Typing Async Functions
2167
2110
@@ -2232,10 +2175,7 @@ How can we type `data` as a number without changing the calls to `fetch` or `res
2232
2175
2233
2176
There are two possible solutions here.
2234
2177
2235
- <Exercise
2236
- title="Exercise 7: Typing Async Functions"
2237
- filePath="/src/015-essential-types-and-annotations/038-type-async-functions.problem.ts"
2238
- />
2178
+ <Exercise title =" Exercise 7: Typing Async Functions " filePath =" /src/015-essential-types-and-annotations/038-type-async-functions.problem.ts " ></Exercise >
2239
2179
2240
2180
#### Solution 1: Optional Function Parameters
2241
2181
@@ -2441,7 +2381,7 @@ This works because `data` was `any` before, and `await response.json()` returns
2441
2381
2442
2382
However, the best way to solve this problem is to add a return type to the function. In this case, it should be a ` number ` :
2443
2383
2444
- ``` ts
2384
+ ``` ts twoslash
2445
2385
// @errors: 1064
2446
2386
async function fetchData(): number {
2447
2387
// function body
0 commit comments