Skip to content

Commit 29f8a55

Browse files
committed
feat: add built-in code editor for all examples
1 parent bb19120 commit 29f8a55

File tree

47 files changed

+969
-92
lines changed

Some content is hidden

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

47 files changed

+969
-92
lines changed

docs/1-basics/1-type-annotations/index.md renamed to docs/1-basics/1-type-annotations/index.mdx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
# Introduction to type annotations
1+
---
2+
sidebar_label: 1.1. Type annotations
3+
---
4+
5+
# Type annotations
26

37
In TypeScript, you can specify the type of a variable, function, or class by adding *type annotations*.
48

@@ -142,4 +146,10 @@ Here are some common basic types you can use:
142146

143147
## Exercise 1.1
144148

149+
import Exercise1_1 from '!!raw-loader!./exercise-1.1.ts';
150+
import Exercise1_1_Solution from '!!raw-loader!./exercise-1.1.solution.ts';
151+
145152
Try adding some type annotations yourself.
153+
154+
<CodeEditor codeKey="exercise1.1.1" defaultValue={Exercise1_1} solution={Exercise1_1_Solution} />
155+

docs/1-basics/10-arrays-records-tuples/index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
---
2+
sidebar_label: 1.10. Arrays, records, and tuples
3+
---
4+
15
# Arrays, records and tuples
26

37
## Arrays

docs/1-basics/11-classes/index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
---
2+
sidebar_label: 1.11. Classes
3+
---
4+
15
# Classes
26

37
Classes are a way to implement object-oriented programming in JavaScript. They are automatically a type in TypeScript.

docs/1-basics/12-readonly-types-as-const/index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
---
2+
sidebar_label: 1.12. Readonly types and `as const`
3+
---
4+
15
# Readonly types and `as const`
26

37
In TypeScript, you can use `as const` to declare that an object is immutable.

docs/1-basics/2-inferred-types/index.md renamed to docs/1-basics/2-inferred-types/index.mdx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
---
2+
sidebar_label: 1.2. Inferred types
3+
---
4+
15
# Inferred types
26

37
> Reference: https://www.typescriptlang.org/docs/handbook/type-inference.html
48
59
Take a look at the following piece of TypeScript code:
610

711
```ts
8-
912
function getUserGreeting(name: string, age: number): string {
1013
const isUserOld: boolean = age > 60;
1114
const isNameCapitalized: boolean = name[0] === name[0].toUpperCase();
@@ -14,7 +17,6 @@ function getUserGreeting(name: string, age: number): string {
1417
// do more stuff
1518
// return greeting
1619
}
17-
1820
```
1921

2022
In the above example, we have annotated the types of `isUserOld`, `isNameCapitalized`, and `isAgeEven` to be `boolean`. However, we can remove these type annotations and TypeScript will still be able to infer the types of these variables.
@@ -52,4 +54,9 @@ These cases will be covered in later chapters. For now, when it is clear what a
5254

5355
## Exercise 2.1
5456

57+
import Exercise2_1 from '!!raw-loader!./exercise-2.1.ts';
58+
import Exercise2_1_Solution from '!!raw-loader!./exercise-2.1.solution.ts';
59+
5560
Make the code more concise by identifying and removing unnecessary type annotations.
61+
62+
<CodeEditor codeKey="exercise1.2.1" defaultValue={Exercise2_1} solution={Exercise2_1_Solution} />

docs/1-basics/3-overriding-inferred-types/index.md renamed to docs/1-basics/3-overriding-inferred-types/index.mdx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
---
2+
sidebar_label: 1.3. Overriding inferred types
3+
---
4+
15
# Overriding inferred types
26

37
Sometimes, you will need to override the types that TypeScript infers. This can be done by explicitly annotating the type of a variable or function.
@@ -37,7 +41,6 @@ const [name, setName] = useState(null);
3741

3842
// TypeScript only allows us to set `name` to `null`
3943
setName("Alice"); // Error: Argument of type 'string' is not assignable to parameter of type 'SetStateAction<null>'
40-
4144
```
4245

4346
Instead, we explicitly say that `name` can be either a `string` or `null`:
@@ -79,4 +82,9 @@ It is also easier to understand our code because our variable is immutable. Try
7982
8083
## Exercise 3.1
8184

82-
Add explicit type annotations to fix the TypeScript errors
85+
import Exercise3_1 from '!!raw-loader!./exercise-3.1.ts';
86+
import Exercise3_1_Solution from '!!raw-loader!./exercise-3.1.solution.ts';
87+
88+
Add explicit type annotations to fix the TypeScript errors.
89+
90+
<CodeEditor codeKey="exercise1.3.1" defaultValue={Exercise3_1} solution={Exercise3_1_Solution} />

docs/1-basics/4-type-aliases/index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
---
2+
sidebar_label: 1.4. Type aliases
3+
---
4+
15
# Type aliases
26

37
Sometimes, you have to reuse a type elsewhere in your code. Instead of repeating the type definition, you can use a type alias.

docs/1-basics/5-object-types-and-interfaces/index.md renamed to docs/1-basics/5-object-types-and-interfaces/index.mdx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
---
2+
sidebar_label: 1.5. Object types (and interfaces)
3+
---
4+
15
# Object types (and interfaces)
26

37
Often in JavaScript, you have something like this:
@@ -99,3 +103,12 @@ type UserWithRole = User & {
99103
}
100104
// ^ UserWithRole: { name: string, age: number, role: string }
101105
```
106+
107+
## Exercise 5.1
108+
109+
import Exercise5_1 from '!!raw-loader!./exercise-5.1.ts';
110+
import Exercise5_1_Solution from '!!raw-loader!./exercise-5.1.solution.ts';
111+
112+
Define object types and interfaces to make the code more structured and readable.
113+
114+
<CodeEditor codeKey="exercise1.5.1" defaultValue={Exercise5_1} solution={Exercise5_1_Solution} />

docs/1-basics/6-optional-properties-and-parameters/index.md renamed to docs/1-basics/6-optional-properties-and-parameters/index.mdx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
---
2+
sidebar_label: 1.6. Optional properties and parameters
3+
---
4+
15
# Optional properties and parameters
26

37
In TypeScript, optional types and parameters allow you to define values that may or may not be present. This is useful when you want to create flexible interfaces or functions that can work with varying amounts of information.
@@ -61,3 +65,12 @@ function getUsername(user: { name: string; username?: string }) {
6165
```
6266

6367
This returns `user.username` if it exists, otherwise it falls back to `user.name`.
68+
69+
## Exercise 6.1
70+
71+
import Exercise6_1 from '!!raw-loader!./exercise-6.1.ts';
72+
import Exercise6_1_Solution from '!!raw-loader!./exercise-6.1.solution.ts';
73+
74+
Add type annotations where necessary and modify the functions to handle optional parameters appropriately.
75+
76+
<CodeEditor codeKey="exercise1.6.1" defaultValue={Exercise6_1} solution={Exercise6_1_Solution} />

docs/1-basics/7-union-and-literal-types/index.md renamed to docs/1-basics/7-union-and-literal-types/index.mdx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
---
2+
sidebar_label: 1.7. Union and literal types
3+
---
4+
15
# Union and Literal Types
26

37
TypeScript provides powerful type system features that allow for more precise type definitions. Two such features are union types and literal types.
@@ -122,3 +126,12 @@ function getAction(direction: 'left' | 'right' | 'up' | 'down'): Action {
122126
- Union types allow you to combine multiple types into one
123127
- Literal types allow you to specify the exact value that a variable can have
124128
- Literal unions allow you to create an enum-like data type in TypeScript
129+
130+
## Exercise 7.1
131+
132+
import Exercise7_1 from '!!raw-loader!./exercise-7.1.ts';
133+
import Exercise7_1_Solution from '!!raw-loader!./exercise-7.1.solution.ts';
134+
135+
Use union and literal types to make the code more robust and type-safe.
136+
137+
<CodeEditor codeKey="exercise1.7.1" defaultValue={Exercise7_1} solution={Exercise7_1_Solution} />

0 commit comments

Comments
 (0)