Skip to content

Commit d75e3d6

Browse files
committed
Added remaining exercises
1 parent e362487 commit d75e3d6

16 files changed

+377
-12
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ yarn exercise 01
1010

1111
## How to take the course
1212

13-
First, start up the [video on YouTube](TODO).
14-
1513
You'll notice that the course is split into exercises. Each exercise is split into a `*.problem.ts` and a `*.solution.ts`.
1614

1715
To take an exercise:
@@ -23,7 +21,7 @@ The `exercise` script will run TypeScript typechecks and a test suite on the exe
2321

2422
This course encourages **active, exploratory learning**. In the video, I'll explain a problem, and **you'll be asked to try to find a solution**. To attempt a solution, you'll need to:
2523

26-
1. Check out [Zod's docs](https://github.com/colinhacks/zod)
24+
1. Check out [TypeScript's docs](https://www.typescriptlang.org/docs/handbook/intro.html)
2725
2. Try to find something that looks relevant.
2826
3. Give it a go to see if it solves the problem.
2927

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "1.0.0",
44
"main": "index.js",
55
"author": "Matt Pocock <mattpocockvoice@gmail.com>",
6-
"license": "GPT-3",
6+
"license": "MIT",
77
"devDependencies": {
88
"@types/node": "^18.6.5",
99
"chokidar": "^3.5.3",
@@ -13,7 +13,9 @@
1313
},
1414
"scripts": {
1515
"exercise": "node scripts/exercise.js",
16-
"solution": "SOLUTION=true node scripts/exercise.js"
16+
"e": "yarn exercise",
17+
"solution": "SOLUTION=true node scripts/exercise.js",
18+
"s": "yarn solution"
1719
},
1820
"dependencies": {
1921
"@types/express": "^4.17.13",

scripts/exercise.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ if (!exercise) {
1515

1616
const allExercises = fs.readdirSync(srcPath);
1717

18-
let suffix = ".problem.ts";
18+
let pathIndicator = ".problem.";
1919

2020
if (process.env.SOLUTION) {
21-
suffix = ".solution.ts";
21+
pathIndicator = ".solution.";
2222
}
2323

2424
const exercisePath = allExercises.find(
2525
(exercisePath) =>
26-
exercisePath.startsWith(exercise) && exercisePath.endsWith(suffix),
26+
exercisePath.startsWith(exercise) && exercisePath.includes(pathIndicator),
2727
);
2828

2929
if (!exercisePath) {
@@ -35,12 +35,17 @@ const exerciseFile = path.resolve(srcPath, exercisePath);
3535

3636
// One-liner for current directory
3737
chokidar.watch(exerciseFile).on("all", (event, path) => {
38+
const fileContents = fs.readFileSync(exerciseFile, "utf8");
39+
40+
const containsVitest = fileContents.includes("vitest");
3841
try {
3942
console.clear();
40-
console.log("Running tests...");
41-
execSync(`vitest run ${exerciseFile} --passWithNoTests`, {
42-
stdio: "inherit",
43-
});
43+
if (containsVitest) {
44+
console.log("Running tests...");
45+
execSync(`vitest run ${exerciseFile} --passWithNoTests`, {
46+
stdio: "inherit",
47+
});
48+
}
4449
console.log("Checking types...");
4550
execSync(`tsc ${exerciseFile} --noEmit --strict`, {
4651
stdio: "inherit",
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { expect, it } from "vitest";
2+
3+
interface User {
4+
id: number;
5+
firstName: string;
6+
lastName: string;
7+
isAdmin: boolean;
8+
}
9+
10+
/**
11+
* How do we ensure that defaultUser is of type User
12+
* at THIS LINE - not further down in the code?
13+
*/
14+
const defaultUser = {};
15+
16+
const getUserId = (user: User) => {
17+
return user.id;
18+
};
19+
20+
it("Should get the user id", () => {
21+
expect(getUserId(defaultUser)).toEqual(1);
22+
});
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { expect, it } from "vitest";
2+
3+
interface User {
4+
id: number;
5+
firstName: string;
6+
lastName: string;
7+
isAdmin: boolean;
8+
}
9+
10+
/**
11+
* How do we ensure that defaultUser is of type User
12+
* at THIS LINE - not further down in the code?
13+
*/
14+
const defaultUser: User = {
15+
id: 1,
16+
firstName: "Matt",
17+
lastName: "Pocock",
18+
isAdmin: true,
19+
};
20+
21+
const getUserId = (user: User) => {
22+
return user.id;
23+
};
24+
25+
it("Should get the user id", () => {
26+
expect(getUserId(defaultUser)).toEqual(1);
27+
});

src/06-unions.problem.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
interface User {
2+
id: number;
3+
firstName: string;
4+
lastName: string;
5+
/**
6+
* How do we ensure that role is only one of:
7+
* - 'admin'
8+
* - 'user'
9+
* - 'super-admin'
10+
*/
11+
role: string;
12+
}
13+
14+
export const defaultUser: User = {
15+
id: 1,
16+
firstName: "Matt",
17+
lastName: "Pocock",
18+
// @ts-expect-error
19+
role: "I_SHOULD_NOT_BE_ALLOWED",
20+
};

src/06-unions.solution.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
interface User {
2+
id: number;
3+
firstName: string;
4+
lastName: string;
5+
/**
6+
* How do we ensure that role is only one of:
7+
* - 'admin'
8+
* - 'user'
9+
* - 'super-admin'
10+
*/
11+
role: "admin" | "user" | "super-admin";
12+
}
13+
14+
export const defaultUser: User = {
15+
id: 1,
16+
firstName: "Matt",
17+
lastName: "Pocock",
18+
// @ts-expect-error
19+
role: "I_SHOULD_NOT_BE_ALLOWED",
20+
};

src/07-arrays.problem.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
interface User {
2+
id: number;
3+
firstName: string;
4+
lastName: string;
5+
/**
6+
* How do we ensure that role is only one of:
7+
* - 'admin'
8+
* - 'user'
9+
* - 'super-admin'
10+
*/
11+
role: "admin" | "user" | "super-admin";
12+
posts: Post;
13+
}
14+
15+
interface Post {
16+
id: number;
17+
title: string;
18+
}
19+
20+
export const defaultUser: User = {
21+
id: 1,
22+
firstName: "Matt",
23+
lastName: "Pocock",
24+
role: "admin",
25+
posts: [
26+
{
27+
id: 1,
28+
title: "How I eat so much cheese",
29+
},
30+
],
31+
};

src/07-arrays.solution.1.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
interface User {
2+
id: number;
3+
firstName: string;
4+
lastName: string;
5+
/**
6+
* How do we ensure that role is only one of:
7+
* - 'admin'
8+
* - 'user'
9+
* - 'super-admin'
10+
*/
11+
role: "admin" | "user" | "super-admin";
12+
posts: Post[];
13+
}
14+
15+
interface Post {
16+
id: number;
17+
title: string;
18+
}
19+
20+
export const defaultUser: User = {
21+
id: 1,
22+
firstName: "Matt",
23+
lastName: "Pocock",
24+
role: "admin",
25+
posts: [
26+
{
27+
id: 1,
28+
title: "How I eat so much cheese",
29+
},
30+
],
31+
};

src/07-arrays.solution.2.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
interface User {
2+
id: number;
3+
firstName: string;
4+
lastName: string;
5+
/**
6+
* How do we ensure that role is only one of:
7+
* - 'admin'
8+
* - 'user'
9+
* - 'super-admin'
10+
*/
11+
role: "admin" | "user" | "super-admin";
12+
posts: Array<Post>;
13+
}
14+
15+
interface Post {
16+
id: number;
17+
title: string;
18+
}
19+
20+
export const defaultUser: User = {
21+
id: 1,
22+
firstName: "Matt",
23+
lastName: "Pocock",
24+
role: "admin",
25+
posts: [
26+
{
27+
id: 1,
28+
title: "How I eat so much cheese",
29+
},
30+
],
31+
};

0 commit comments

Comments
 (0)