Skip to Content
Exercises05 TypeScriptExercise: refactor JS → TS + tests

Exercise: refactor JS → TS + tests

Goal

Take a small JavaScript utility and make it safe and maintainable with TypeScript, backed by tests.

Task

Create a utility (or take an existing one you wrote) such as:

  • parsing a user input payload (unknown → validated/narrowed type)
  • mapping API data into a UI-friendly model

Then:

  • add types
  • remove any
  • add Vitest tests

Requirements

  • Use unknown at the boundary (input).
  • Narrow/validate to a safe type before using it.
  • Tests cover happy path + at least 2 edge cases.

Acceptance criteria (entry)

  • No any in the implementation.
  • Tests pass and meaningfully assert behavior.

Stretch goals (mid)

  • Add a generic helper function and justify why it’s worth it.
  • Add a test data factory with typed defaults.

Stretch goals (senior)

  • Explain where you’d enforce this boundary in a Next.js + tRPC feature.
Last updated on