-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Strongly type all Quiz data types [refactor] #11992
Conversation
✅ Deploy Preview for ethereumorg ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good @wackerow good refactor. Left a couple of questions, thanks!
|
||
export type QuestionBank = Record<string, RawQuestion> | ||
export type QuestionKey = keyof typeof allQuestionData | ||
export type AnswerKey = typeof allQuestionData[QuestionKey]["answers"][number]["id"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pretty cool
@@ -887,6 +887,6 @@ const questionBank: QuestionBank = { | |||
], | |||
correctAnswerId: "h005-b", | |||
}, | |||
} | |||
} as const satisfies QuestionBank |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a benefit doing as const satisfies
instead of just satisfies
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried without, but had to add back as const
(in this commit) after it wasn't casting the typeof
as literals to perform keyof
on... instead it was just simplified to string
(IIRC)...
The other place satisfies
is used didn't give me that problem, so I kept as const
off of that one.
quizKey || | ||
Object.keys(allQuizzesData).filter((quizUri) => | ||
window?.location.href.includes(quizUri) | ||
)[0] || |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did a search and it seems that we always pass a quizKey
, this default is never used. I'm ok with removing it if not used, but just curious about its original purpose, do you know it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm honestly not sure... but that was my same reasoning... there was no circumstance where the fallback would be used since a valid key is required.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking a little here, it may have simply been a product of not having proper typing, so any string
could be used which may-or-may-not be available. This shouldn't be an issue now.
const showConfetti = useMemo<boolean>( | ||
() => !!quizData && showResults && isPassingScore, | ||
[quizData, showResults, isPassingScore] | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense 👍🏼
Description
interface
s totype
s; migrate frominterfaces.ts
totypes.ts
QuizKey
,QuestionKey
, andAnswerKey
typesQuizKey
,QuestionKey
, andAnswerKey
types: Implemented usingsatisfies
andkeyof type
pattern to both quiz and question banks. Allows us to create these types based on the strings chosen in each data mapping (keyof typeof
pattern), while maintaining type-checking for child object structure (usingsatisfies
)