Skip to content

Commit d84f78e

Browse files
committed
Added tutorial on Set
1 parent 9f4cd6e commit d84f78e

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/10-set.problem.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { expect, it } from "vitest";
2+
import { Equal, Expect } from "./helpers/type-utils";
3+
4+
const guitarists = new Set();
5+
6+
guitarists.add("Jimi Hendrix");
7+
guitarists.add("Eric Clapton");
8+
9+
it("Should contain Jimi and Eric", () => {
10+
expect(guitarists.has("Jimi Hendrix")).toEqual(true);
11+
expect(guitarists.has("Eric Clapton")).toEqual(true);
12+
});
13+
14+
it("Should give a type error when you try to pass a non-string", () => {
15+
// @ts-expect-error
16+
guitarists.add(2);
17+
});
18+
19+
it("Should be typed as an array of strings", () => {
20+
const guitaristsAsArray = Array.from(guitarists);
21+
22+
type cases = [Expect<Equal<typeof guitaristsAsArray, string[]>>];
23+
});

src/10-set.solution.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { expect, it } from "vitest";
2+
import { Equal, Expect } from "./helpers/type-utils";
3+
4+
const guitarists = new Set<string>();
5+
6+
guitarists.add("Jimi Hendrix");
7+
guitarists.add("Eric Clapton");
8+
9+
it("Should contain Jimi and Eric", () => {
10+
expect(guitarists.has("Jimi Hendrix")).toEqual(true);
11+
expect(guitarists.has("Eric Clapton")).toEqual(true);
12+
});
13+
14+
it("Should give a type error when you try to pass a non-string", () => {
15+
// @ts-expect-error
16+
guitarists.add(2);
17+
});
18+
19+
it("Should be typed as an array of strings", () => {
20+
const guitaristsAsArray = Array.from(guitarists);
21+
22+
type cases = [Expect<Equal<typeof guitaristsAsArray, string[]>>];
23+
});

0 commit comments

Comments
 (0)