What you are practising: adding type annotations to a plain JavaScript file and letting the compiler catch a mistake the runtime never would.
Accept the assignment, clone your repository, then install the tooling once:
npm installIf you have not used npm before: npm install reads package.json, downloads the listed development tools (the TypeScript compiler, ESLint, and Prettier) into a node_modules/ folder, and records the exact versions in package-lock.json. Think of it as Maven resolving your pom.xml, but into a local folder instead of a shared repository.
This is the rhythm for every TypeScript exercise in this module:
- Open a terminal in the
exercise/folder. - Rename the starter:
mv calculator.js calculator.ts(or right-click and rename in VS Code). - Work through what the compiler surfaces. Under strict mode, every untyped parameter is an error, so the squiggles are your to-do list.
- Compile with a bare
npx tsc. Do not pass the filename:npx tsc calculator.tsignorestsconfig.jsoncompletely and compiles with loose defaults, which defeats the point. The bare form picks up this folder's strict configuration. - Run the output:
node calculator.js.
The file contains four functions: add, subtract, multiply, and divide, plus a small test block that calls each of them.
- Rename
calculator.jstocalculator.ts. - Add type annotations to all parameters and return types.
- Fix any issues TypeScript flags. One of the test calls is deliberately wrong; find and fix it once the types make it visible.
- Compile with
npx tscand confirm it produces clean JavaScript. - Run
node calculator.jsand verify the output.
Keep the exported function names (add, subtract, multiply, divide) — the autograder imports them by name.
- Add a fifth function,
percentageOf(part, whole), that returns a percentage as a number. - Make
dividesafe. What should it return when the divisor is zero? How would you express that in the type? - Try removing one of the return type annotations. Is the inferred type what you expected?
npx tsc completes with no errors, node calculator.js prints four correct results, 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.