Skip to content

Commit ddcd788

Browse files
committed
Add destructuring and spread/rest exercises
- Add destructuring exercise (06.destructuring) with object and array steps - Add spread/rest exercise (07.spread-rest) with object spread and rest params - Update workshop README and FINISHED files to include new content
1 parent 8005ffa commit ddcd788

File tree

37 files changed

+1044
-21
lines changed

37 files changed

+1044
-21
lines changed

exercises/02.arrays/01.problem.array-basics/README.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ basic array operations.
1313
💰 Array syntax:
1414

1515
```ts
16-
const items: string[] = ['item1', 'item2', 'item3']
16+
const items: Array<string> = ['item1', 'item2', 'item3']
1717
items.push('item4') // Add to end
1818
items[0] // First element
1919
items.length // Number of elements

exercises/02.arrays/01.solution.array-basics/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Inventory System
22
// Working with basic arrays
33

4-
const products: string[] = ['Laptop', 'Mouse', 'Keyboard']
4+
const products: Array<string> = ['Laptop', 'Mouse', 'Keyboard']
55

66
products.push('Monitor')
77

exercises/02.arrays/02.solution.array-types/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Product Inventory
22
// Arrays of objects
33

4-
const products: { name: string; price: number; inStock: boolean }[] = [
4+
const products: Array<{ name: string; price: number; inStock: boolean }> = [
55
{ name: 'Laptop', price: 999.99, inStock: true },
66
{ name: 'Mouse', price: 29.99, inStock: true },
77
{ name: 'Keyboard', price: 79.99, inStock: false },

exercises/02.arrays/03.solution.iteration/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Inventory Analysis
22
// Iterating and filtering arrays
33

4-
const products: { name: string; price: number; inStock: boolean }[] = [
4+
const products: Array<{ name: string; price: number; inStock: boolean }> = [
55
{ name: 'Laptop', price: 999.99, inStock: true },
66
{ name: 'Mouse', price: 29.99, inStock: true },
77
{ name: 'Keyboard', price: 79.99, inStock: false },
@@ -18,7 +18,11 @@ for (const product of products) {
1818
// Filtering products that are in stock
1919
// This imperative approach works, but there's a more declarative way
2020
// using filter() which we'll learn in Exercise 04!
21-
const inStockProducts: { name: string; price: number; inStock: boolean }[] = []
21+
const inStockProducts: Array<{
22+
name: string
23+
price: number
24+
inStock: boolean
25+
}> = []
2226
for (const product of products) {
2327
if (product.inStock) {
2428
inStockProducts.push(product)

exercises/02.arrays/FINISHED.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
👨‍💼 Great work with arrays! You now know how to:
44

5-
- 📚 Create typed arrays with `Type[]` or `Array<Type>`
5+
- 📚 Create typed arrays with `Array<Type>` (preferred) or `Type[]`
66
- ✅ Get type safety when adding elements
77
- 🔄 Iterate over arrays with `for...of`
88
- 🎭 Use union types for mixed arrays
99

1010
🦉 Array types shine when combined with object types:
1111

1212
```ts
13-
const users: { name: string; age: number }[] = [
13+
const users: Array<{ name: string; age: number }> = [
1414
{ name: 'Alice', age: 30 },
1515
{ name: 'Bob', age: 25 },
1616
]

exercises/02.arrays/README.mdx

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,36 @@ const names: string[] = ['Alice', 'Bob', 'Charlie']
1313
TypeScript offers two ways to write array types:
1414

1515
```ts
16-
// Square bracket syntax (more common)
16+
// Square bracket syntax (shorter)
1717
const numbers: number[] = [1, 2, 3]
1818

19-
// Generic syntax
19+
// Generic syntax (preferred)
2020
const numbers: Array<number> = [1, 2, 3]
2121
```
2222

23-
Both are identical—use whichever you prefer, but be consistent.
23+
Both are functionally identical. The square bracket syntax is shorter and you'll
24+
see it frequently in codebases. However, the generic syntax (`Array<Type>`) is
25+
preferred for several reasons:
26+
27+
- **Readability**: We read left-to-right, so "Array of numbers" reads more
28+
naturally than "numbers... array"
29+
- **Consistency with readonly**: `ReadonlyArray<string>` vs `readonly string[]`
30+
- **Union types**: `Array<string | number>` vs `(string | number)[]` (no
31+
parentheses needed)
32+
- **Works better with `keyof`**: Avoids confusing operator precedence issues
33+
34+
For a deeper dive into why generic notation is better, check out
35+
[Array Types in TypeScript](https://tkdodo.eu/blog/array-types-in-type-script)
36+
by TkDodo.
37+
38+
We'll use the generic syntax going forward in this workshop.
2439

2540
## Type Safety with Arrays
2641

2742
TypeScript ensures you only put the right types in arrays:
2843

2944
```ts
30-
const numbers: number[] = [1, 2, 3]
45+
const numbers: Array<number> = [1, 2, 3]
3146
numbers.push(4) // ✅ OK
3247
numbers.push('five') // ❌ Error: 'string' not assignable to 'number'
3348
```
@@ -38,11 +53,11 @@ You can add elements to arrays in two ways:
3853

3954
```ts
4055
// Mutation (modifies the original array)
41-
const numbers: number[] = [1, 2, 3]
56+
const numbers: Array<number> = [1, 2, 3]
4257
numbers.push(4)
4358

4459
// Immutable (creates a new array)
45-
const numbers: number[] = [1, 2, 3]
60+
const numbers: Array<number> = [1, 2, 3]
4661
const moreNumbers = [...numbers, 4]
4762
```
4863

@@ -63,7 +78,7 @@ state.
6378
Need different types? Use union types:
6479

6580
```ts
66-
const mixed: (string | number)[] = [1, 'two', 3, 'four']
81+
const mixed: Array<string | number> = [1, 'two', 3, 'four']
6782
```
6883

6984
In this exercise, you'll create and work with typed arrays.

exercises/03.tuples/02.problem.tuple-patterns/README.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const [result, success] = divide(10, 2)
2323
💰 Return tuple pattern:
2424

2525
```ts
26-
function getMinMax(nums: number[]): [number, number] {
26+
function getMinMax(nums: Array<number>): [number, number] {
2727
return [Math.min(...nums), Math.max(...nums)]
2828
}
2929
```

exercises/03.tuples/02.solution.tuple-patterns/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function parseNumber(str: string): [number, boolean] {
66
return [num, !isNaN(num)]
77
}
88

9-
function getMinMax(nums: number[]): [number, number] {
9+
function getMinMax(nums: Array<number>): [number, number] {
1010
return [Math.min(...nums), Math.max(...nums)]
1111
}
1212

exercises/03.tuples/README.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ position**. They're like arrays, but more structured.
55

66
```ts
77
// Array: any number of strings
8-
const names: string[] = ['Alice', 'Bob']
8+
const names: Array<string> = ['Alice', 'Bob']
99

1010
// Tuple: exactly two elements, string then number
1111
const person: [string, number] = ['Alice', 30]

exercises/04.array-methods/01.problem.map/README.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const result = array.map((element) => transformedValue)
2525
💰 TypeScript infers the result type from what you return:
2626

2727
```ts
28-
const names = products.map((p) => p.name) // string[]
28+
const names = products.map((p) => p.name) // Array<string>
2929
```
3030

3131
📜 [MDN - Array.prototype.map()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)

0 commit comments

Comments
 (0)