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

Commit 34d465e

Browse files
authored
Todo app 6 (#15)
* Wordsmith * Clarify readonly * Typo * Wordsmith
1 parent a1fe8c9 commit 34d465e

File tree

4 files changed

+36
-14
lines changed

4 files changed

+36
-14
lines changed

snippets/snippets/todo/kuzw.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function completeAll(todos: Todo[]): Todo[] {
2+
// We want it to return a new array
3+
// instead of modifying the original array
4+
}

snippets/snippets/todo/lgci.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Same as before
1+
// Same as before: Each property is readonly
22
type Todo = Readonly<{
33
id: number
44
text: string

src/lib/snippets.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,12 @@ function toggleTodo(
530530
// ...
531531
}`
532532

533-
export const lgci = `// Same as before
533+
export const kuzw = `function completeAll(todos: Todo[]): Todo[] {
534+
// We want it to return a new array
535+
// instead of modifying the original array
536+
}`
537+
538+
export const lgci = `// Same as before: Each property is readonly
534539
type Todo = Readonly<{
535540
id: number
536541
text: string

src/pages/todo.tsx

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ const Page = () => (
212212
/>
213213
<P>
214214
As you can see, when you check/uncheck a checkbox, it updates the
215-
underlying data (the <Code>done</Code> property), and in turn the
215+
underlying data (the <Code>done</Code> property), and in turn, the
216216
UI gets updated. This is how UI libraries like React and Vue work.
217217
</P>
218218
<EmojiSeparator
@@ -226,7 +226,7 @@ const Page = () => (
226226
description={
227227
<>
228228
When the user interacts with the UI, the data gets updated,
229-
and in turn the UI gets updated
229+
and in turn, the UI gets updated
230230
</>
231231
}
232232
/>
@@ -356,7 +356,7 @@ const Page = () => (
356356
<strong>Now, here’s a question:</strong>{' '}
357357
<Highlight>How can we prevent mistakes like this?</Highlight>{' '}
358358
Little Duckling is a junior developer, but we want to make sure
359-
that he succeeds at his job by helping him make less mistakes.
359+
that he succeeds at his job by helping him make fewer mistakes.
360360
</P>
361361
<EmojiSeparator
362362
emojis={['sweat', 'chickEgg', 'sweat']}
@@ -797,7 +797,7 @@ const Page = () => (
797797
/>
798798
<P>
799799
Yes! In TypeScript, you can use keywords like{' '}
800-
<Code>Readonly&lt;...&gt;</Code> to conovert one type into another
800+
<Code>Readonly&lt;...&gt;</Code> to convert one type into another
801801
type—in this case, it creates a new type with{' '}
802802
<Code>readonly</Code> properties.
803803
</P>
@@ -827,7 +827,7 @@ const Page = () => (
827827
<P>
828828
<strong>
829829
1. We can define a <Code>type</Code> to make sure that the input
830-
and the output of a function is of the correct type.
830+
and the output of a function are of the correct type.
831831
</strong>
832832
</P>
833833
<CodeBlock
@@ -841,7 +841,7 @@ const Page = () => (
841841
<P>
842842
<strong>
843843
2. We can use the <Code>readonly</Code> keyword to make sure
844-
that an object property is not modified.
844+
that an object’s properties are not modified.
845845
</strong>
846846
</P>
847847
<CodeBlock
@@ -979,13 +979,25 @@ const Page = () => (
979979
/>
980980
<P>
981981
<strong>Third,</strong> we want to make sure that{' '}
982-
<Code>completeAll()</Code> returns a new array and does NOT modify
983-
the original array. Each item in the array is already{' '}
984-
<Code>readonly</Code>, but <em>the array itself</em> is NOT{' '}
985-
<Code>readonly</Code> yet.
982+
<Highlight>
983+
<Code>completeAll()</Code> returns a new array and does NOT
984+
modify the original array.
985+
</Highlight>
986986
</P>
987+
<CodeBlock
988+
snippet={snippets.kuzw}
989+
shouldHighlight={lineIndex => lineIndex === 1}
990+
/>
987991
<P>
988-
To make the array itself <Code>readonly</Code>,{' '}
992+
Because we defined <Code>Todo</Code> earlier using{' '}
993+
<Code>Readonly&lt;...&gt;</Code>, each todo item in the array is
994+
already <Code>readonly</Code>. However, <em>the array itself</em>{' '}
995+
is NOT <Code>readonly</Code> yet.
996+
</P>
997+
<P>
998+
<strong>
999+
To make the array itself <Code>readonly</Code>,
1000+
</strong>{' '}
9891001
<Highlight>
9901002
we’ll add the <Code>readonly</Code> keyword to{' '}
9911003
<Code>Todo[]</Code> like so:
@@ -1017,7 +1029,8 @@ const Page = () => (
10171029
Yes, Little Duckling! We use the <Code>readonly</Code> keyword for
10181030
arrays. And by doing so,{' '}
10191031
<Highlight>
1020-
TypeScript will prevent you from accidently modifying the array.
1032+
TypeScript will prevent you from accidentally modifying the
1033+
array.
10211034
</Highlight>
10221035
</P>
10231036
<CodeBlock

0 commit comments

Comments
 (0)