What you are practising: union types, literal types, and optional parameters, by making a forgiving JavaScript utility strict.
Accept the assignment, clone your repository, then install the tooling once:
npm installSame rhythm as session 1:
- Open a terminal in the
exercise/folder. - Rename the starter:
mv playlist.js playlist.ts. - Work through what the compiler surfaces.
- Compile with a bare
npx tsc. Notnpx tsc playlist.ts: passing a filename makestscignoretsconfig.jsonand its strict settings entirely. - Run the output:
node playlist.js.
The file is a small utility for managing a music playlist. It contains four functions: addTrack, removeTrack, findTracksByMood, and describePlaylist. Two of them accept union-typed input and one has an optional parameter.
- Rename
playlist.jstoplaylist.ts. - Add type annotations to every function signature.
- Use union and literal types where the inputs can be one of several shapes.
- Use optional parameters where the input is genuinely optional.
- Fix any issues TypeScript flags. The JavaScript version of
describePlaylisthas a bug that only becomes visible once you add types. Run the starter first and look closely at its output if you want a head start. - Compile with
npx tscand confirm it produces clean JavaScript. - Run
node playlist.jsand verify the output.
Keep the exported function names (addTrack, removeTrack, findTracksByMood, describePlaylist) — the autograder imports them by name.
- Extract the track shape into a type alias (a spoiler for session 3, but if you already know the syntax, go for it).
- Make the
moodfield a literal union ('upbeat' | 'chill' | 'focus'). What changes infindTracksByMood? - Add a fifth function,
shuffle, that takes a playlist and returns a new shuffled playlist. What should its signature be?
npx tsc completes with no errors, node playlist.js prints a sensible summary (no NaN in sight), and your work is committed and pushed.
Every push runs two GitHub Actions checks:
- Type check — runs
npx tscon your exercise under the strict settings, once you have renamed the starter to.ts. - Autograding — runs an automated test suite (Vitest) against your functions and reports a functionality score, plus an automated code-quality review.