Skip to content
This repository was archived by the owner on Sep 29, 2025. It is now read-only.

Commit 5e32d5c

Browse files
authored
Todo app 2 (#11)
* Wordsmith * Recap
1 parent 21e2a3c commit 5e32d5c

File tree

4 files changed

+94
-2
lines changed

4 files changed

+94
-2
lines changed

snippets/snippets/todo/frtm.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
type Todo = {
2+
id: number
3+
text: string
4+
done: boolean
5+
}
6+
7+
// Make sure that the input and the output
8+
// is of the correct type
9+
function toggleTodo(todo: Todo): Todo {
10+
// ...
11+
}

snippets/snippets/todo/npah.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
type Todo = {
2+
readonly id: number
3+
readonly text: string
4+
readonly done: boolean
5+
}
6+
7+
function toggleTodo(todo: Todo): Todo {
8+
// This won’t compile
9+
todo.done = !todo.done
10+
return todo
11+
}

src/lib/snippets.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,18 @@ export const dxfc = `// Associated data. If we're using React, this
502502
{ id: 2, text: 'Second todo', done: false }
503503
]`
504504

505+
export const frtm = `type Todo = {
506+
id: number
507+
text: string
508+
done: boolean
509+
}
510+
511+
// Make sure that the input and the output
512+
// is of the correct type
513+
function toggleTodo(todo: Todo): Todo {
514+
// ...
515+
}`
516+
505517
export const lieq = `type Todo = {
506518
id: number
507519
text: string
@@ -527,6 +539,18 @@ export const njgr = `function toggleTodo(todo: Todo): Todo {
527539
return todo
528540
}`
529541

542+
export const npah = `type Todo = {
543+
readonly id: number
544+
readonly text: string
545+
readonly done: boolean
546+
}
547+
548+
function toggleTodo(todo: Todo): Todo {
549+
// This won’t compile
550+
todo.done = !todo.done
551+
return todo
552+
}`
553+
530554
export const ntau = `function toggleTodo(todo: Todo): Todo {
531555
// Little Duckling’s code from earlier:
532556
// Missing the "id" property

src/pages/todo.tsx

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -634,15 +634,15 @@ const Page = () => (
634634
children: (
635635
<>
636636
<P>
637-
Oops, I did it again! <Emoji type="sweat" />
637+
Oops, I messed up again! <Emoji type="sweat" />
638638
</P>
639639
</>
640640
)
641641
}
642642
]}
643643
></BubbleQuotes>
644644
<P>
645-
No worries! <strong>The question is</strong>,{' '}
645+
No worries, Little Duckling! <strong>The question is</strong>,{' '}
646646
<Highlight>
647647
how can we use TypeScript to prevent a mistake like this?
648648
</Highlight>
@@ -719,6 +719,52 @@ const Page = () => (
719719
</>
720720
)
721721
},
722+
{
723+
color: 'green',
724+
title: <>Types are like lightweight, automatic unit tests</>,
725+
content: (
726+
<>
727+
<P>So far, we’ve learned the following:</P>
728+
<P>
729+
<strong>
730+
1. We can define a <Code>type</Code> to make sure that the input
731+
and the output of a function is of the correct type.
732+
</strong>
733+
</P>
734+
<CodeBlock
735+
snippet={snippets.frtm}
736+
shouldHighlight={(lineNumber, tokenNumber) =>
737+
(lineNumber === 0 && tokenNumber <= 2) ||
738+
(lineNumber === 8 &&
739+
((tokenNumber >= 5 && tokenNumber <= 7) ||
740+
tokenNumber === 11))
741+
}
742+
/>
743+
<P>
744+
<strong>
745+
2. We can use the <Code>readonly</Code> keyword to make sure
746+
that an object property is not modified.
747+
</strong>
748+
</P>
749+
<CodeBlock
750+
snippet={snippets.npah}
751+
shouldHighlight={(lineIndex, tokenIndex) =>
752+
lineIndex >= 1 && lineIndex <= 3 && tokenIndex === 1
753+
}
754+
/>
755+
<P>
756+
Before TypeScript, you needed to write unit tests to verify these
757+
things. So in a sense,{' '}
758+
<Highlight>
759+
TypeScript’s types act as lightweight unit tests that run
760+
automatically every time you save (compile) the code.
761+
</Highlight>{' '}
762+
It helps you write less buggy code with very little overhead.
763+
</P>
764+
<P>Next, let’s take a look at more non-trivial examples!</P>
765+
</>
766+
)
767+
},
722768
// {
723769
// title: (
724770
// <>

0 commit comments

Comments
 (0)