Skip to content

Commit

Permalink
2023-09-06T09:28:44.713Z
Browse files Browse the repository at this point in the history
  • Loading branch information
mattpocock committed Sep 6, 2023
1 parent a6c174c commit cc19403
Show file tree
Hide file tree
Showing 89 changed files with 303 additions and 122 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
.vscode
tsconfig.temp.json
*.prompt.*
*.prompt.*
dist
121 changes: 0 additions & 121 deletions src/030-classes/example.ts

This file was deleted.

56 changes: 56 additions & 0 deletions src/032-typescript-only-features/115-enums.problem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { expect, it, vi } from "vitest";

const LogLevel = {
DEBUG: 0,
INFO: 1,
WARN: 2,
ERROR: 3,
};

function log(opts: { globalLogLevel: number; level: number; message: string }) {
if (opts.level >= opts.globalLogLevel) {
console.log(opts.message);
}
}

it("Should log if the level is higher than the global log level", () => {
const consoleLog = vi.spyOn(console, "log");
log({
globalLogLevel: LogLevel.INFO,
level: LogLevel.ERROR,
message: "Hello!",
});

expect(consoleLog).toHaveBeenCalledWith("Hello!");
});

it("Should log if the level is equal to the global log level", () => {
const consoleLog = vi.spyOn(console, "log");
log({
globalLogLevel: LogLevel.INFO,
level: LogLevel.INFO,
message: "Hello!",
});

expect(consoleLog).toHaveBeenCalledWith("Hello!");
});

it("Should not log if the level is lower than the global log level", () => {
const consoleLog = vi.spyOn(console, "log");
log({
globalLogLevel: LogLevel.INFO,
level: LogLevel.DEBUG,
message: "Hello!",
});

expect(consoleLog).not.toHaveBeenCalled();
});

it("Should give you a TS error if you pass an invalid log level", () => {
log({
globalLogLevel: LogLevel.INFO,
// @ts-expect-error
level: 123,
message: "Hello!",
});
});
60 changes: 60 additions & 0 deletions src/032-typescript-only-features/115-enums.solution.1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { expect, it, vi } from "vitest";

enum LogLevel {
DEBUG,
INFO,
WARN,
ERROR,
}

function log(opts: {
globalLogLevel: LogLevel;
level: LogLevel;
message: string;
}) {
if (opts.level >= opts.globalLogLevel) {
console.log(opts.message);
}
}

it("Should log if the level is higher than the global log level", () => {
const consoleLog = vi.spyOn(console, "log");
log({
globalLogLevel: LogLevel.INFO,
level: LogLevel.ERROR,
message: "Hello!",
});

expect(consoleLog).toHaveBeenCalledWith("Hello!");
});

it("Should log if the level is equal to the global log level", () => {
const consoleLog = vi.spyOn(console, "log");
log({
globalLogLevel: LogLevel.INFO,
level: LogLevel.INFO,
message: "Hello!",
});

expect(consoleLog).toHaveBeenCalledWith("Hello!");
});

it("Should not log if the level is lower than the global log level", () => {
const consoleLog = vi.spyOn(console, "log");
log({
globalLogLevel: LogLevel.INFO,
level: LogLevel.DEBUG,
message: "Hello!",
});

expect(consoleLog).not.toHaveBeenCalled();
});

it("Should give you a TS error if you pass an invalid log level", () => {
log({
globalLogLevel: LogLevel.INFO,
// @ts-expect-error
level: 123,
message: "Hello!",
});
});
60 changes: 60 additions & 0 deletions src/032-typescript-only-features/115-enums.solution.2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { expect, it, vi } from "vitest";

enum LogLevel {
DEBUG = 0,
INFO = 1,
WARN = 2,
ERROR = 3,
}

function log(opts: {
globalLogLevel: LogLevel;
level: LogLevel;
message: string;
}) {
if (opts.level >= opts.globalLogLevel) {
console.log(opts.message);
}
}

it("Should log if the level is higher than the global log level", () => {
const consoleLog = vi.spyOn(console, "log");
log({
globalLogLevel: LogLevel.INFO,
level: LogLevel.ERROR,
message: "Hello!",
});

expect(consoleLog).toHaveBeenCalledWith("Hello!");
});

it("Should log if the level is equal to the global log level", () => {
const consoleLog = vi.spyOn(console, "log");
log({
globalLogLevel: LogLevel.INFO,
level: LogLevel.INFO,
message: "Hello!",
});

expect(consoleLog).toHaveBeenCalledWith("Hello!");
});

it("Should not log if the level is lower than the global log level", () => {
const consoleLog = vi.spyOn(console, "log");
log({
globalLogLevel: LogLevel.INFO,
level: LogLevel.DEBUG,
message: "Hello!",
});

expect(consoleLog).not.toHaveBeenCalled();
});

it("Should give you a TS error if you pass an invalid log level", () => {
log({
globalLogLevel: LogLevel.INFO,
// @ts-expect-error
level: 123,
message: "Hello!",
});
});
44 changes: 44 additions & 0 deletions src/032-typescript-only-features/116-string-enums.problem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { it } from "vitest";

const Method = {
GET: "GET",
POST: "POST",
PUT: "PUT",
DELETE: "DELETE",
};

const request = (url: string, method: "GET" | "POST" | "PUT" | "DELETE") => {
// ...
};

it("Should force you to use the enum values", () => {
request(
"https://example.com",
// @ts-expect-error
"GET",
);

request(
"https://example.com",
// @ts-expect-error
"POST",
);

request("https://example.com", Method.GET);
request("https://example.com", Method.POST);
});

it("Should give you an error if you pass a different enum with the same value", () => {
enum Method2 {
GET = "GET",
POST = "POST",
PUT = "PUT",
DELETE = "DELETE",
}

request(
"https://example.com",
// @ts-expect-error
Method2.GET,
);
});
44 changes: 44 additions & 0 deletions src/032-typescript-only-features/116-string-enums.solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { it } from "vitest";

enum Method {
GET = "GET",
POST = "POST",
PUT = "PUT",
DELETE = "DELETE",
}

const request = (url: string, method: Method) => {
// ...
};

it("Should force you to use the enum values", () => {
request(
"https://example.com",
// @ts-expect-error
"GET",
);

request(
"https://example.com",
// @ts-expect-error
"POST",
);

request("https://example.com", Method.GET);
request("https://example.com", Method.POST);
});

it("Should give you an error if you pass a different enum with the same value", () => {
enum Method2 {
GET = "GET",
POST = "POST",
PUT = "PUT",
DELETE = "DELETE",
}

request(
"https://example.com",
// @ts-expect-error
Method2.GET,
);
});
Loading

0 comments on commit cc19403

Please sign in to comment.