Skip to content

Commit f02919a

Browse files
committed
add object type aliases and stuff
1 parent 6944bb3 commit f02919a

File tree

46 files changed

+351
-63
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+351
-63
lines changed

exercises/01.objects/01.problem.object-literals/README.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ objects with proper types.
88
1. Create a `user` object with `name`, `age`, and `email` properties
99
2. Create a second user object using the same structure
1010

11-
🦺 Object type syntax (We'll cover this in the Type Safety workshop later):
11+
🦺 Object type syntax (we'll name these types in a later step):
1212

1313
```ts
1414
const obj: { prop1: Type1; prop2: Type2 } = {

exercises/01.objects/01.problem.object-literals/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
// console.log(`Admin: ${admin.name}, Age: ${admin.age}`)
1313

1414
// 🐨 Export your variables so we can verify your work
15-
// 💰 export { user, admin }
15+
// export { user, admin }

exercises/01.objects/02.problem.property-access/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@ const product = {
99
}
1010

1111
// 🐨 Log the product name using dot notation
12-
// 💰 console.log(product.name)
1312

1413
// 🐨 Log the product price using bracket notation
15-
// 💰 console.log(product['price'])
1614

1715
// 🐨 Create a function `formatProduct` that takes a product object
1816
// and returns a string like "TypeScript Handbook - $29.99"
@@ -24,4 +22,4 @@ const product = {
2422
// console.log(formatProduct(product))
2523

2624
// 🐨 Export your variables and functions so we can verify your work
27-
// 💰 export { product, formatProduct }
25+
// export { product, formatProduct }
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Object Types
2+
3+
👨‍💼 We're repeating the same object shape in a few places. Let's give that
4+
shape a name so we can reuse it.
5+
6+
🦺 A **type alias** lets us name an object type. We'll go deeper on type aliases
7+
in the Type Safety workshop, but this small intro will make later steps easier.
8+
9+
🐨 Open <InlineFile file="index.ts" /> and:
10+
11+
1. Create a `Task` type alias with `id`, `title`, and `completed`
12+
2. Create two task objects using that type
13+
14+
📜 [TypeScript Handbook - Type Aliases](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#type-aliases)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Task Board
2+
// Naming object types
3+
4+
// 🐨 Create a type called `Task` with:
5+
// - id: number
6+
// - title: string
7+
// - completed: boolean
8+
// 💰 type Task = { ... }
9+
10+
// 🐨 Create a `firstTask` and `secondTask` using the Task type
11+
// - firstTask: id 1, title "Write tests", completed false
12+
// - secondTask: id 2, title "Ship release", completed true
13+
14+
// 🐨 Export your variables so we can verify your work
15+
// export { firstTask, secondTask }
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "exercises_01.objects_03.problem.object-types",
3+
"type": "module",
4+
"scripts": {
5+
"start": "npx @kentcdodds/log-module@latest ./index.ts",
6+
"test": "node --test index.test.ts"
7+
}
8+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Object Types
2+
3+
👨‍💼 Nice work! You created a reusable object type with a `type` alias and used
4+
it to build multiple tasks.
5+
6+
🦉 We'll go deeper on type aliases (and when to prefer them) in the Type Safety
7+
workshop, but naming object shapes already makes your code easier to read.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import assert from 'node:assert/strict'
2+
import { test } from 'node:test'
3+
import * as solution from './index.ts'
4+
5+
await test('firstTask is exported', () => {
6+
assert.ok(
7+
'firstTask' in solution,
8+
'🚨 Make sure you export "firstTask" - add: export { firstTask, ... }',
9+
)
10+
})
11+
12+
await test('firstTask has expected values', () => {
13+
assert.deepStrictEqual(
14+
solution.firstTask,
15+
{ id: 1, title: 'Write tests', completed: false },
16+
'🚨 firstTask should match the required id, title, and completed values',
17+
)
18+
})
19+
20+
await test('secondTask is exported', () => {
21+
assert.ok(
22+
'secondTask' in solution,
23+
'🚨 Make sure you export "secondTask" - add: export { firstTask, secondTask, ... }',
24+
)
25+
})
26+
27+
await test('secondTask has expected values', () => {
28+
assert.deepStrictEqual(
29+
solution.secondTask,
30+
{ id: 2, title: 'Ship release', completed: true },
31+
'🚨 secondTask should match the required id, title, and completed values',
32+
)
33+
})
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Task Board
2+
// Naming object types
3+
4+
type Task = {
5+
id: number
6+
title: string
7+
completed: boolean
8+
}
9+
10+
const firstTask: Task = {
11+
id: 1,
12+
title: 'Write tests',
13+
completed: false,
14+
}
15+
16+
const secondTask: Task = {
17+
id: 2,
18+
title: 'Ship release',
19+
completed: true,
20+
}
21+
22+
export { firstTask, secondTask }
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "exercises_01.objects_03.solution.object-types",
3+
"type": "module",
4+
"scripts": {
5+
"start": "npx @kentcdodds/log-module@latest ./index.ts",
6+
"test": "node --test index.test.ts"
7+
}
8+
}

0 commit comments

Comments
 (0)