Skip to content
This repository was archived by the owner on Sep 29, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
0375dea
CompletedTodo
chibicode Dec 8, 2019
9308118
Intersection types
chibicode Dec 9, 2019
e92038f
Implement completeAll()
chibicode Dec 9, 2019
af3948b
Summary and section headers
chibicode Dec 9, 2019
440a60c
Add example to slide 14
chibicode Dec 11, 2019
8897eaa
Done with section 2
chibicode Dec 11, 2019
927b8ae
Start separator
chibicode Dec 11, 2019
254bd74
Finish slide 20
chibicode Dec 11, 2019
a9cb54f
Addition to slide 20
chibicode Dec 11, 2019
a23cd33
Update showMarkAllAsCompleted
chibicode Dec 11, 2019
ba9a47e
Start slide 21
chibicode Dec 11, 2019
81ec40f
Finish slide 21
chibicode Dec 12, 2019
165e413
Start slide 22
chibicode Dec 12, 2019
0073257
Continue with todo
chibicode Dec 12, 2019
bacbf5e
Optional properties
chibicode Dec 14, 2019
0e06369
Remove completeAll() rewrite
chibicode Dec 14, 2019
dc42ee4
SImplify
chibicode Dec 14, 2019
f31124a
Remove optional properties from section 1
chibicode Dec 14, 2019
984e61d
Start location labels
chibicode Dec 14, 2019
99927d5
Completely remove separator
chibicode Dec 14, 2019
d762c30
Location labels
chibicode Dec 15, 2019
78b82e4
Done with slide 21
chibicode Dec 15, 2019
a375807
Start slide 22
chibicode Dec 15, 2019
1ee3cd6
Done with slide 21
chibicode Dec 15, 2019
f9c4f9f
Continue with slide 22-23
chibicode Dec 15, 2019
932b5cd
Remove react
chibicode Dec 15, 2019
970bcbf
Comment
chibicode Dec 15, 2019
321ab46
Clarify slide 22
chibicode Dec 15, 2019
5e615f5
Start slide 23
chibicode Dec 16, 2019
1de18e5
Compile failure
chibicode Dec 16, 2019
62fb06e
Finish slide 23
chibicode Dec 16, 2019
65b4865
Update slide 23
chibicode Dec 16, 2019
1a6bbac
Simplify
chibicode Dec 16, 2019
ae5a4e5
Finish slide 24
chibicode Dec 16, 2019
55c37f2
Update intro
chibicode Dec 17, 2019
4681d5e
Wordsmith
chibicode Dec 17, 2019
ab31941
Simplify
chibicode Dec 17, 2019
1804124
Wordsmith
chibicode Dec 17, 2019
91c1ce3
Wordsmith slide 1
chibicode Dec 17, 2019
f68ed4d
Wordsmith slide 3
chibicode Dec 17, 2019
5672a27
Start rewriting slide 4
chibicode Dec 17, 2019
90d7daf
Update about me
chibicode Dec 17, 2019
1c908f0
Wordsmith slide 4
chibicode Dec 17, 2019
ebec8e8
Finish slide 5
chibicode Dec 17, 2019
9a90a99
Wordsmith slide 6
chibicode Dec 17, 2019
90fb84c
Finish slide 7
chibicode Dec 17, 2019
840061d
Wordsmith slide 8
chibicode Dec 17, 2019
bc740c7
Wordsmith slide 10
chibicode Dec 17, 2019
8115699
Wordsmith 12
chibicode Dec 17, 2019
94eb9a6
Slide 13 wordsmith
chibicode Dec 17, 2019
54edbed
Wordsmith 14
chibicode Dec 17, 2019
71dc2fc
Up to slide 16
chibicode Dec 18, 2019
6950504
Wordsmith slide 17
chibicode Dec 18, 2019
038c3b5
Wordsmith slide 19
chibicode Dec 18, 2019
83b09f3
Wordsmith slide 21
chibicode Dec 18, 2019
ec1027f
Done with wordsmith
chibicode Dec 18, 2019
897ef41
Add to articleKeys
chibicode Dec 18, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
"options": {
"printWidth": 48
}
},
{
"files": ["snippets/snippets/**/longerWidth/*.ts"],
"options": {
"printWidth": 53
}
}
]
}
1 change: 1 addition & 0 deletions snippets/bin/generateSnippetsBundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const regenerate = () => {
const contents = fs.readFileSync(file, 'utf8')
return `export const ${file
.replace(/\.\/snippets\/snippets\/\w+\//, '')
.replace(/longerWidth\//, '')
.replace(/\.ts/, '')} = \`${contents
.trim()
.replace(/^;/m, '')
Expand Down
5 changes: 5 additions & 0 deletions snippets/snippets/todo/bpmz.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type CompletedTodo = Readonly<{
id: number
text: string
done: true
}>
2 changes: 1 addition & 1 deletion snippets/snippets/todo/csum.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// todo must match the Todo type
// Parameter "todo" must match the Todo type
function toggleTodo(todo: Todo) {
// ...
}
2 changes: 1 addition & 1 deletion snippets/snippets/todo/dxfc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Associated data. If we're using React, this
// would be the todo component’s props or state
// would be the component’s props or state
;[
{ id: 1, text: 'First todo', done: false },
{ id: 2, text: 'Second todo', done: false }
Expand Down
5 changes: 5 additions & 0 deletions snippets/snippets/todo/eega.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
else {
// place = 'work' or { custom: string }, and
// place.custom is invalid if place = 'work'
return 'pinEmoji ' + place.custom
}
2 changes: 1 addition & 1 deletion snippets/snippets/todo/frtm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ type Todo = {
}

// Make sure that the input and the output
// is of the correct type
// are of the correct type (both must be Todo)
function toggleTodo(todo: Todo): Todo {
// ...
}
8 changes: 8 additions & 0 deletions snippets/snippets/todo/hquv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
;[
// ...
// ...
// ...
// ...
// No place property
{ id: 5, text: 'Read a book', done: false }
]
8 changes: 8 additions & 0 deletions snippets/snippets/todo/hszk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function completeAll(
todos: readonly Todo[]
): CompletedTodo[] {
return todos.map(todo => ({
...todo,
done: true
}))
}
15 changes: 0 additions & 15 deletions snippets/snippets/todo/jkjo.ts

This file was deleted.

7 changes: 0 additions & 7 deletions snippets/snippets/todo/lgci.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
// Same as before: Each property is readonly
type Todo = Readonly<{
id: number
text: string
done: boolean
}>

// Input is an array of Todo items: Todo[]
function completeAll(todos: Todo[]) {
// ...
Expand Down
17 changes: 17 additions & 0 deletions snippets/snippets/todo/longerWidth/dhor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
type Place = 'home' | 'work' | { custom: string }

// TypeScript knows what the type of "place"
// would be at each point inside the function
function placeToString(place: Place): string {
// In here, place = 'home', 'work' or { custom:… }

if (place === 'home') {
// In here, place = 'home'

return 'homeEmoji Home'
} else {
// In here, place = 'work' or { custom: string }

return 'pinEmoji ' + place.custom
}
}
7 changes: 7 additions & 0 deletions snippets/snippets/todo/longerWidth/fawy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type Place = 'home' | 'work' | { custom: string }

// They all compile
const place1: Place = 'home'
const place2: Place = 'work'
const place3: Place = { custom: 'Gym' }
const place4: Place = { custom: 'Supermarket' }
18 changes: 18 additions & 0 deletions snippets/snippets/todo/longerWidth/ntup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// If we have a variable that’s a union type…
type Place = 'home' | 'work' | { custom: string }

function placeToString(place: Place): string {
// TypeScript is smart about what the variable’s
// possible values are for each branch of if/else

if (place === 'home') {
// TypeScript knows place = 'home' here
// (So it won’t compile if you do place.custom)
} else if (place === 'work') {
// TypeScript knows place = 'work' here
// (So it won’t compile if you do place.custom)
} else {
// TypeScript knows place = { custom: … } here
// (So you can do place.custom)
}
}
9 changes: 9 additions & 0 deletions snippets/snippets/todo/longerWidth/rvyq.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
type Place = 'home' | 'work' | { custom: string }

type Todo = Readonly<{
id: number
text: string
done: boolean
// place is optional
place?: Place
}>
11 changes: 11 additions & 0 deletions snippets/snippets/todo/longerWidth/szco.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Correct implementation
function placeToString(place: Place): string {
if (place === 'home') {
return 'homeEmoji Home'
} else if (place === 'work') {
return 'workEmoji Work'
} else {
// place is guaranteed to be { custom: string }
return 'pinEmoji ' + place.custom
}
}
1 change: 1 addition & 0 deletions snippets/snippets/todo/longerWidth/umjt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type Place = 'home' | 'work' | { custom: string }
10 changes: 10 additions & 0 deletions snippets/snippets/todo/longerWidth/vgja.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
type Place = 'home' | 'work' | { custom: string }

// Little Duckling’s implementation
function placeToString(place: Place): string {
if (place === 'home') {
return 'homeEmoji Home'
} else {
return 'pinEmoji ' + place.custom
}
}
13 changes: 13 additions & 0 deletions snippets/snippets/todo/longerWidth/wymp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const argument = {
id: 1,
text: '…',
done: true
}

console.log('Before toggleTodo(), argument is:')
console.log(argument)

toggleTodo(argument)

console.log('After toggleTodo(), argument is:')
console.log(argument)
2 changes: 1 addition & 1 deletion snippets/snippets/todo/mwrj.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// After declaring todos as: readonly Todo[],
// After declaring todos as readonly Todo[],
// the following code WILL NOT compile:

// Compile error - modifies the array
Expand Down
5 changes: 5 additions & 0 deletions snippets/snippets/todo/mxqy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type Todo = Readonly<{
id: number
text: string
done: boolean
}>
7 changes: 7 additions & 0 deletions snippets/snippets/todo/mzyn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Creates a union type of number and string
type Foo = number | string

// You can assign either a number or a string
// variable to Foo. So these will both compile:
const a: Foo = 1
const b: Foo = 'hello'
2 changes: 1 addition & 1 deletion snippets/snippets/todo/njgr.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
function toggleTodo(todo: Todo): Todo {
// Little Duckling’s refactoring is a
// bad refactoring because it modifies
// the original todo object
// the argument (input) todo object
todo.done = !todo.done
return todo
}
6 changes: 6 additions & 0 deletions snippets/snippets/todo/npgx.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type Todo = Readonly<{
id: number
text: string
done: boolean
place: Place
}>
7 changes: 7 additions & 0 deletions snippets/snippets/todo/npog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type Todo = Readonly<{
id: number
text: string
done: boolean
// place is optional
place?: Place
}>
6 changes: 6 additions & 0 deletions snippets/snippets/todo/okva.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
console.log(
completeAll([
{ id: 1, text: '…', done: false },
{ id: 2, text: '…', done: true }
])
)
6 changes: 6 additions & 0 deletions snippets/snippets/todo/oone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Returns an array where "done" is all true
function completeAll(
todos: readonly Todo[]
): CompletedTodo[] {
// ...
}
5 changes: 2 additions & 3 deletions snippets/snippets/todo/qbgu.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Returns a new todo object
// with the opposite boolean value
// for the "done" proprty.
// We said earlier that
// toggleTodo must return a new todo object.
function toggleTodo(todo) {
// ...
}
11 changes: 11 additions & 0 deletions snippets/snippets/todo/qnrh.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
placeToString('home')
// __home__

placeToString('work')
// __work__

placeToString({ custom: 'Gym' })
// __gym__

placeToString({ custom: 'Supermarket' })
// __supermarket__
11 changes: 11 additions & 0 deletions snippets/snippets/todo/qnwc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// They booth have a property foo,
// but B’s foo (true) is
// more specific than A’s foo (boolean)
type A = { foo: boolean }
type B = { foo: true }

// This intersection type…
type AandB = A & B

// …is equivalent to:
type AandB = { foo: true }
11 changes: 11 additions & 0 deletions snippets/snippets/todo/rlya.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
type Todo = Readonly<{
id: number
text: string
done: boolean
}>

type CompletedTodo = Readonly<{
id: number
text: string
done: true
}>
10 changes: 10 additions & 0 deletions snippets/snippets/todo/rmuo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
type Todo = Readonly<{
id: number
text: string
done: boolean
}>

// Override the done property of Todo
type CompletedTodo = Todo & {
readonly done: true
}
5 changes: 5 additions & 0 deletions snippets/snippets/todo/ruga.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function completeAll(
todos: readonly Todo[]
): CompletedTodo[] {
// ...
}
10 changes: 8 additions & 2 deletions snippets/snippets/todo/vgnq.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
// This will continue to work because
// the input todo is not modified
type Todo = {
readonly id: number
readonly text: string
readonly done: boolean
}

// Earlier implementation: it will continue to
// work because the input todo is not modified
function toggleTodo(todo: Todo): Todo {
return {
id: todo.id,
Expand Down
19 changes: 11 additions & 8 deletions snippets/snippets/todo/vpco.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
// Returns a new todo object with the opposite
// Takes a single todo object and returns
// a new todo object containing the opposite
// boolean value for the "done" proprty.
function toggleTodo(todo) {
// Case 1: If todo is
// { id: …, text: '…', done: true }, return
// { id: …, text: '…', done: false }
//
// Case 2: If todo is
// { id: …, text: '…', done: false }, return
// { id: …, text: '…', done: true }
// ...
}

// Example usage:

toggleTodo({ id: …, text: '…', done: true })
// -> returns { id: …, text: '…', done: false }

toggleTodo({ id: …, text: '…', done: false })
// -> returns { id: …, text: '…', done: true }
11 changes: 11 additions & 0 deletions snippets/snippets/todo/wdjp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
type A = { a: number }
type B = { b: string }

// This intersection type…
type AandB = A & B

// …is equivalent to:
type AandB = {
a: number
b: string
}
9 changes: 9 additions & 0 deletions snippets/snippets/todo/whae.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function completeAll(
todos: readonly Todo[]
): CompletedTodo[] {
return todos.map(todo => ({
...todo,
// What if we set done to false?
done: false
}))
}
16 changes: 0 additions & 16 deletions snippets/snippets/todo/wymp.ts

This file was deleted.

Loading