Skip to content

cbfacademy/ts-exercises-core-types

Repository files navigation

Session 2: Core types

What you are practising: union types, literal types, and optional parameters, by making a forgiving JavaScript utility strict.

Setup

Accept the assignment, clone your repository, then install the tooling once:

npm install

The workflow

Same rhythm as session 1:

  1. Open a terminal in the exercise/ folder.
  2. Rename the starter: mv playlist.js playlist.ts.
  3. Work through what the compiler surfaces.
  4. Compile with a bare npx tsc. Not npx tsc playlist.ts: passing a filename makes tsc ignore tsconfig.json and its strict settings entirely.
  5. Run the output: node playlist.js.

Your task

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.

  1. Rename playlist.js to playlist.ts.
  2. Add type annotations to every function signature.
  3. Use union and literal types where the inputs can be one of several shapes.
  4. Use optional parameters where the input is genuinely optional.
  5. Fix any issues TypeScript flags. The JavaScript version of describePlaylist has 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.
  6. Compile with npx tsc and confirm it produces clean JavaScript.
  7. Run node playlist.js and verify the output.

Keep the exported function names (addTrack, removeTrack, findTracksByMood, describePlaylist) — the autograder imports them by name.

Stretch goals

  • 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 mood field a literal union ('upbeat' | 'chill' | 'focus'). What changes in findTracksByMood?
  • Add a fifth function, shuffle, that takes a playlist and returns a new shuffled playlist. What should its signature be?

Done when

npx tsc completes with no errors, node playlist.js prints a sensible summary (no NaN in sight), and your work is committed and pushed.

How your work is graded

Every push runs two GitHub Actions checks:

  • Type check — runs npx tsc on 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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages