Skip to content

Commit 008331d

Browse files
committed
Improve exercises
1 parent 1a7107c commit 008331d

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

08-using-everything/exercises.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// This an ESLint issue, not a typescript issue, you can trust other errors :)
77
type TriggerESLintIssue = `${"a"}`;
88

9-
import { Equal, Expect, TODO } from "../helpers";
9+
import { Compute, Equal, Expect, TODO } from "../helpers";
1010

1111
/**
1212
* 1. Implement a `ExtractUrlParamNames<url>` generic
@@ -36,10 +36,12 @@ namespace two {
3636
type ExtractUrlParams<url> = TODO;
3737

3838
type res1 = ExtractUrlParams<"/user/:username">;
39-
type test1 = Expect<Equal<res1, { username: string }>>;
39+
type test1 = Expect<Equal<Compute<res1>, { username: string }>>;
4040

4141
type res2 = ExtractUrlParams<"/user/:username/post/:postId">;
42-
type test2 = Expect<Equal<res2, { username: string; postId: string }>>;
42+
type test2 = Expect<
43+
Equal<Compute<res2>, { username: string; postId: string }>
44+
>;
4345
}
4446

4547
namespace bonus {
@@ -54,10 +56,20 @@ namespace bonus {
5456
type ExtractUrlParams<url> = TODO;
5557

5658
type res3 = ExtractUrlParams<"/dashboard(/:dashboardId)">;
57-
type test3 = Expect<Equal<res3, { dashboardId?: string }>>;
59+
type test3 = Expect<Equal<Compute<res3>, { dashboardId?: string }>>;
5860

5961
type res4 = ExtractUrlParams<"org/:orgId/dashboard(/:dashboardId)">;
60-
type test4 = Expect<Equal<res4, { orgId: string; dashboardId?: string }>>;
62+
type test4 = Expect<
63+
Equal<Compute<res4>, { orgId: string; dashboardId?: string }>
64+
>;
65+
type res5 =
66+
ExtractUrlParams<"org/:orgId/dashboard(/:dashboardId/widgets/:widgetId)">;
67+
type test5 = Expect<
68+
Equal<
69+
Compute<res5>,
70+
{ orgId: string; dashboardId?: string; widgetId?: string }
71+
>
72+
>;
6173
}
6274

6375
/**

08-using-everything/solution.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// This an ESLint issue, not a typescript issue, you can trust other errors :)
77
type TriggerESLintIssue = `${"a"}`;
88

9-
import { Equal, Expect } from "../helpers";
9+
import { Compute, Equal, Expect } from "../helpers";
1010

1111
/**
1212
* 1. Implement a `ExtractUrlParamNames<url>` generic
@@ -52,10 +52,12 @@ namespace two {
5252
: {};
5353

5454
type res1 = ExtractUrlParams<"/user/:username">;
55-
type test1 = Expect<Equal<res1, { username: string }>>;
55+
type test1 = Expect<Equal<Compute<res1>, { username: string }>>;
5656

5757
type res2 = ExtractUrlParams<"/user/:username/post/:postId">;
58-
type test2 = Expect<Equal<res2, { username: string } & { postId: string }>>;
58+
type test2 = Expect<
59+
Equal<Compute<res2>, { username: string; postId: string }>
60+
>;
5961
}
6062

6163
namespace bonus {
@@ -76,11 +78,20 @@ namespace bonus {
7678
: {};
7779

7880
type res3 = ExtractUrlParams<"/dashboard(/:dashboardId)">;
79-
type test3 = Expect<Equal<res3, { dashboardId?: string }>>;
81+
type test3 = Expect<Equal<Compute<res3>, { dashboardId?: string }>>;
8082

8183
type res4 = ExtractUrlParams<"org/:orgId/dashboard(/:dashboardId)">;
8284
type test4 = Expect<
83-
Equal<res4, { orgId: string } & { dashboardId?: string }>
85+
Equal<Compute<res4>, { orgId: string; dashboardId?: string }>
86+
>;
87+
88+
type res5 =
89+
ExtractUrlParams<"org/:orgId/dashboard(/:dashboardId/widgets/:widgetId)">;
90+
type test5 = Expect<
91+
Equal<
92+
Compute<res5>,
93+
{ orgId: string; dashboardId?: string; widgetId?: string }
94+
>
8495
>;
8596
}
8697

helpers.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@ export type Expect<T extends true> = T;
1515
export type Tuple = [any, ...any[]];
1616

1717
const TODO = Symbol("TODO");
18-
export type TODO = any & typeof TODO;
18+
export type TODO = typeof TODO;
1919

20-
// { a: string } & { b: string } -> { a: string, b: string }
20+
/**
21+
* Compute is a helper converting intersections of objects into
22+
* flat, plain object types.
23+
*
24+
* @example
25+
* Compute<{ a: string } & { b: string }> -> { a: string, b: string }
26+
*/
2127
export type Compute<obj> = { [k in keyof obj]: obj[k] } & unknown;

0 commit comments

Comments
 (0)