Skip to content

Commit aa0e8b1

Browse files
committed
Added solutions to 02
1 parent e50340a commit aa0e8b1

File tree

2 files changed

+40
-17
lines changed

2 files changed

+40
-17
lines changed

exercises/02-roleBasedAccess.exercise.ts

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,11 @@ export const canUserAccess = (role: Role, action: Action) => {
112112
* ^ 🚁
113113
*
114114
* 🚁 Hover Action. Now, we're getting ALL members of ALL of the arrays.
115-
* Fabulous.
115+
*
116+
* 🕵️‍♂️ Discuss amongst yourselves WHY you think [number] works. When you
117+
* think you've figured something out, check:
118+
*
119+
* Solution #1
116120
*/
117121

118122
/**
@@ -185,22 +189,12 @@ export const canUserAccess = (role: Role, action: Action) => {
185189
* possibleActions.includes(action as any)
186190
*
187191
* It works? Nice.
188-
*/
189-
190-
/**
191-
* 💡 Often, when doing more advanced typings, you're going to find
192-
* that casting to any is the most productive solution in your arsenal.
193-
* Many libraries doing advanced TS work, like TRPC and Zod, use 'any'
194-
* liberally.
195-
*
196-
* Because TypeScript is fundamentally not a sound type system (because
197-
* JavaScript itself is unsound), you will occasionally need to use any's
198-
* in _some places_ in your apps. My opinion is that the best place for
199-
* them is hidden away in useful functions, like the one above. Any's for
200-
* the function creator, not the function consumer.
201-
*
202-
* For the curious - yes, I found a different solution -
203-
* ReadonlyArray<Action> - which I'll explain in the break.
192+
*
193+
* 🕵️‍♂️ Discuss amongst yourselves: is this a good solution? What
194+
* problems could you imagine coming up against for this? Should
195+
* any _ever_ be used?
196+
*
197+
* For my thoughts, see Solution 2:
204198
*/
205199

206200
/**
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Solution #1
3+
*
4+
* 💡 The reason that this works:
5+
*
6+
* type Action = UserAccessModelValues[number];
7+
*
8+
* Is because number, in this position, represents a union
9+
* type of ALL possible numbers. So TypeScript uses this
10+
* as a shortcut to say "this is how you"
11+
*/
12+
13+
/**
14+
* Solution #2
15+
*
16+
* 💡 Often, when doing more advanced typings, you're going to find
17+
* that casting to any is the most productive solution in your arsenal.
18+
* Many libraries doing advanced TS work, like TRPC and Zod, use 'any'
19+
* liberally.
20+
*
21+
* Because TypeScript is fundamentally not a sound type system (because
22+
* JavaScript itself is unsound), you will occasionally need to use any's
23+
* in _some places_ in your apps. My opinion is that the best place for
24+
* them is hidden away in useful functions, like the one above. Any's for
25+
* the function creator, not the function consumer.
26+
*
27+
* For the curious - yes, I found a different solution -
28+
* ReadonlyArray<Action> - which I'll explain in the break.
29+
*/

0 commit comments

Comments
 (0)