- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Core Tech
Arrays and Collections in TypeScript
You will build the inventory module behind a small warehouse: typed arrays of stock records that must sometimes be copied and sometimes mutated in place, search and reporting functions that must not run on invalid data, tuples describing exactly where an item sits and whether it has a live sensor reading, and finally Set and Map-backed catalogs of unique tags and reorder thresholds that feed a single inventory report.
Lab Info
Table of Contents
-
Challenge
Step 1: Tour the warehouse inventory project and its watched build
Globomantics runs a small warehouse, and the tracker behind it needs an inventory module: stock records that sometimes must be copied and sometimes mutated in place, searches that refuse to run on bad data, bin coordinates that have a fixed shape, and catalogs of tags and reorder points that never lose track of a missing key. Over the next four steps you will build that module in TypeScript, one file at a time, finishing with a single report that pulls every piece together. (Fictional names only. No affiliation with or endorsement by any real company.) The project already defines the shared shapes you will work against: a
StockItemrecord with asku,name,quantity, andtagsinsrc/inventoryArray.ts, plus the bin and sensor tuple types you will meet in Step 4 insrc/binLocation.ts. You will fill in four modules insrc/:inventoryArray.tsfor copy-vs-mutate array operations,inventorySearch.tsfor validated search and chained reporting,binLocation.tsfor tuple-based location and sensor modeling, andinventoryCollections.tsfor Set- and Map-backed catalogs that feed the final report. Each file's tasks are grouped together and build on the ones before them. TypeScript's compiler checks your types, but the test runner does not: it strips types without checking them. This project turns onstrictmode intsconfig.json, and every task's test runs the compiler first, so a type error fails the task before any behavior is checked. To check your types at any time, run this in the Terminal:npm run typecheckWhen the command prints nothing, your types are clean. Otherwise, each error names the file, the line, and the rule that failed, such as
src/binLocation.ts(15,3): error TS2322. Fix the first error first, since later ones often follow from it. Each step's tasks are graded by a matching test file:tests/step-2.test.tsexercises Step 2's array helpers,tests/step-3.test.tsexercises Step 3's search functions, and so on, with one test function per task named after it, such astask 2.1andtask 2.2. You do not need to open these files to make progress, but knowing that a specific test backs a specific task is useful when you want to understand exactly what behavior is expected before you start typing. -
Challenge
Step 2: Build the foundational array helpers
The tracker's first job is keeping stock counts accurate, and that means being deliberate about when an operation returns a new array and when it changes the one it was given.
src/inventoryArray.tsholds three such operations, starting with a restock that must never disturb the array a caller is still holding onto. Not every operation should copy. Marking an item as depleted is the kind of change every part of the system should see immediately, which calls for an intentional mutation instead. Sorting sits in the middle: it looks read-only, butsortmutates its receiver, so producing an ordered view still requires the copying discipline from the first task. -
Challenge
Step 3: Validate, search, and chain transformations
With the array helpers in place, the tracker needs to search and report on stock without ever running that logic against data that cannot support it.
src/inventorySearch.tsstarts with the most basic search: finding one item by SKU, guarded against an empty catalog and an unknown SKU alike. Reports usually need more than one item, and more than one operation. Chainingfilter,map, andsortin a fixed order, rather than folding them into a single loop, keeps each step easy to read and easy to test on its own.reducefinishes the set: once a list is filtered down to the items that matter, reducing it to a single total is the natural next step, provided the input that produced the filter is validated first. -
Challenge
Step 4: Model bin locations and sensor readings with tuples
A bin location is not a loose bag of numbers. It is exactly three values, in a fixed order, and TypeScript's tuples let you say so.
src/binLocation.tsmodelsBinLocationas a[aisle, shelf, bin]tuple; the first task turns one of those tuples into a sentence a warehouse worker can read. Sensor readings add a wrinkle: a sensor that is not in contact with anything reportsnull, not a fake zero. ASensorReadingtuple's Celsius element is typednumber | nullfor exactly that reason, and converting it safely means checking fornullbefore doing any arithmetic. Putting a bin and its sensor together calls for a tuple nested inside a tuple:BinCoordinates, areadonly [BinLocation, SensorReading | undefined], where the sensor slot may be entirely absent for a bin that was never wired up.readonlyalso means the compiler stops you from writing back into either position by mistake. -
Challenge
Step 5: Track unique tags and reorder thresholds, then assemble the report
The last file,
src/inventoryCollections.ts, tracks two things a plain array handles poorly: a set of tags that should never contain a duplicate, and a lookup table of reorder thresholds keyed by SKU. ASet<string>is the natural fit for the first, since adding a value that is already present is a no-op. AMap<string, number>fits the thresholds, but only if missing keys are handled deliberately:Map.getreturnsundefinedfor a key that was never set, and a stored value of0must not be mistaken for that same 'missing' case. Every piece built across the last steps now has one job left: come together into a single report. The final function calls back into the search and Set logic already written, rather than re-deriving any of it. -
Challenge
Step 6: Run and check the application
Every task is done, and each one was graded the moment you completed it. What you have not done yet is watch the whole application run at once, which is the only place the pieces you wrote separately become a single working program.
Start the application
In the Terminal tab, start the dev server:
npm run devIt serves the application on port 5173.
In the Web Browser tab, click its Refresh to view the application. The page is running the functions you implemented in the previous steps, so what you see is your own code rather than a demonstration of it.
The page lists the stock sorted by quantity, shows the lookups for AX-200 and the safety tag, describes a bin location and its sensor, and ends with an inventory report of tags, low-stock items, and total quantity. Select Restock AX-200 (+5) and the quantity for AX-200 rises by 5.
The server reloads on save, so you can change a file and see the result without restarting anything. When you are done looking around, press
Control+Cin the Terminal to stop the server and free the terminal for the checks below. ### Run the checks yourselfThe lab ran each task's test for you as you went, but they are ordinary tests and you can run them whenever you like. The whole suite:
npx --no-install vitest run --reporter=dotOr one task at a time:
bash runTest.sh "task 2.1"Each failure names the task it belongs to, so the output tells you which step to go back to rather than which line to stare at. ### Run the TypeScript compiler
The tests check behavior, while the compiler checks the types across every file at once. With all twelve tasks done, run it on your finished code in the Terminal:
npm run typecheckAfter the two lines npm prints for the script, the command returns to the prompt with no errors listed. That means
strictmode found no type errors insrc/ortests/. Two things are worth trying before you finish. First, break something on purpose and watch the matching check fail, then put it back. Knowing what a failure looks like is worth as much as knowing what a pass looks like, and it costs you nothing here.Second, ask the compiler to catch a type mistake. In
src/binLocation.ts, change the first line ofdescribeBinLocation's body toreturn 42;, then run the compiler again:npm run typecheckThe compiler reports that type
numberis not assignable to typestring, andbash runTest.sh "task 4.1"fails on that error before it checks any output. Put your code back when you are done. Congratulations on completing the Globomantics inventory tracker.You built the inventory module behind it: array helpers that copy or mutate on purpose, search and report functions that validate their input, tuple models for bin locations and sensor readings, and Set- and Map-backed catalogs that feed a single inventory report. The page you ran in the browser calls every one of those functions.
You also ran TypeScript's strict compiler over all of it and watched it catch a type mistake before any test did. Choosing between copying and mutating, guarding input before searching, and handling
nulland missing keys deliberately are habits that carry over to any TypeScript codebase.
About the author
Real skill practice before real-world application
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.