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
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
4 changes: 4 additions & 0 deletions snippets/snippets/todo/kuzw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function completeAll(todos: Todo[]): Todo[] {
// We want it to return a new array
// instead of modifying the original array
}
2 changes: 1 addition & 1 deletion snippets/snippets/todo/lgci.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Same as before
// Same as before: Each property is readonly
type Todo = Readonly<{
id: number
text: string
Expand Down
7 changes: 6 additions & 1 deletion src/lib/snippets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,12 @@ function toggleTodo(
// ...
}`

export const lgci = `// Same as before
export const kuzw = `function completeAll(todos: Todo[]): Todo[] {
// We want it to return a new array
// instead of modifying the original array
}`

export const lgci = `// Same as before: Each property is readonly
type Todo = Readonly<{
id: number
text: string
Expand Down
37 changes: 25 additions & 12 deletions src/pages/todo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ const Page = () => (
/>
<P>
As you can see, when you check/uncheck a checkbox, it updates the
underlying data (the <Code>done</Code> property), and in turn the
underlying data (the <Code>done</Code> property), and in turn, the
UI gets updated. This is how UI libraries like React and Vue work.
</P>
<EmojiSeparator
Expand All @@ -226,7 +226,7 @@ const Page = () => (
description={
<>
When the user interacts with the UI, the data gets updated,
and in turn the UI gets updated
and in turn, the UI gets updated
</>
}
/>
Expand Down Expand Up @@ -356,7 +356,7 @@ const Page = () => (
<strong>Now, here’s a question:</strong>{' '}
<Highlight>How can we prevent mistakes like this?</Highlight>{' '}
Little Duckling is a junior developer, but we want to make sure
that he succeeds at his job by helping him make less mistakes.
that he succeeds at his job by helping him make fewer mistakes.
</P>
<EmojiSeparator
emojis={['sweat', 'chickEgg', 'sweat']}
Expand Down Expand Up @@ -797,7 +797,7 @@ const Page = () => (
/>
<P>
Yes! In TypeScript, you can use keywords like{' '}
<Code>Readonly&lt;...&gt;</Code> to conovert one type into another
<Code>Readonly&lt;...&gt;</Code> to convert one type into another
type—in this case, it creates a new type with{' '}
<Code>readonly</Code> properties.
</P>
Expand Down Expand Up @@ -827,7 +827,7 @@ const Page = () => (
<P>
<strong>
1. We can define a <Code>type</Code> to make sure that the input
and the output of a function is of the correct type.
and the output of a function are of the correct type.
</strong>
</P>
<CodeBlock
Expand All @@ -841,7 +841,7 @@ const Page = () => (
<P>
<strong>
2. We can use the <Code>readonly</Code> keyword to make sure
that an object property is not modified.
that an object’s properties are not modified.
</strong>
</P>
<CodeBlock
Expand Down Expand Up @@ -979,13 +979,25 @@ const Page = () => (
/>
<P>
<strong>Third,</strong> we want to make sure that{' '}
<Code>completeAll()</Code> returns a new array and does NOT modify
the original array. Each item in the array is already{' '}
<Code>readonly</Code>, but <em>the array itself</em> is NOT{' '}
<Code>readonly</Code> yet.
<Highlight>
<Code>completeAll()</Code> returns a new array and does NOT
modify the original array.
</Highlight>
</P>
<CodeBlock
snippet={snippets.kuzw}
shouldHighlight={lineIndex => lineIndex === 1}
/>
<P>
To make the array itself <Code>readonly</Code>,{' '}
Because we defined <Code>Todo</Code> earlier using{' '}
<Code>Readonly&lt;...&gt;</Code>, each todo item in the array is
already <Code>readonly</Code>. However, <em>the array itself</em>{' '}
is NOT <Code>readonly</Code> yet.
</P>
<P>
<strong>
To make the array itself <Code>readonly</Code>,
</strong>{' '}
<Highlight>
we’ll add the <Code>readonly</Code> keyword to{' '}
<Code>Todo[]</Code> like so:
Expand Down Expand Up @@ -1017,7 +1029,8 @@ const Page = () => (
Yes, Little Duckling! We use the <Code>readonly</Code> keyword for
arrays. And by doing so,{' '}
<Highlight>
TypeScript will prevent you from accidently modifying the array.
TypeScript will prevent you from accidentally modifying the
array.
</Highlight>
</P>
<CodeBlock
Expand Down