Examenzaal is a practice-exam site for Dutch exams. The real exams mix question types: plain multiple choice, "tick all that apply", a grid where every row gets one column, a typed number, putting items in order, dragging items onto targets, and clicking a region on a picture. Our scoring code has to handle all of them, and it has to score the way the exam bodies do, where they publish how they score. Here is how that ended up as one small function, and the handful of rules that keep it honest.
One union for the answer key, one for the answer
Every question in an assembled exam carries an answer key, and the key is a tagged union with seven type tags:
export type AnswerKey = | { type : "single_choice" ; correctOptionId : string } | { type : "numeric" ; min : number ; max : number } | { type : "numeric" ; values : number [ ] } | { type : "sleep" ; placements : Placements } | { type : "hotspot" ; correctRegionId : string } | { type : "multi_select" ; correctOptionIds : string [ ] } | { type : "matrix" ;…