- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Core Tech
Navigate Typed DOM Trees in TypeScript
Your team runs standups from a single HTML page listing agenda topics, each of which can have nested sub-points. The page shell already exists, but the interactive behavior is missing: nothing walks the tree, nothing responds to clicks, and nothing renders new items. You will implement the TypeScript layer that makes the agenda board navigable and editable directly in the browser.
Lab Info
Table of Contents
-
Challenge
Step 1: Open the Standup Agenda Board in the Browser
Open the Standup Agenda Board in the Browser
Your team runs standups from one page: an agenda of topics, where each topic can carry nested sub-points, so a standup reads as a tree instead of a flat list. The page shell already exists. What is missing is the TypeScript that makes the tree respond to clicks and keystrokes.
The project is a small Vite app.
index.htmlholds only a<div id="root">mount point, andsrc/main.tsxmounts theAppcomponent fromsrc/App.tsx. Before it does,main.tsxalso makes one plain DOM change of its own, which you will check below.Apprenders the heading, an Add topic button and an empty<ul class="agenda-list">, then fills that list with three topics using functions you will write:createAgendaItem,attachAddHandler,attachRemoveHandler,attachEnterKeyHandlerandgetTextInput. It also callsinsertItemAt,findEnclosingAgendaItemandtoggleDoneStatewhen you use the board.src/styles.cssalready styles the classes the board uses, such as.agenda-item,.sub-itemsand.agenda-item-done, so the result of your code shows without any CSS from you. You won't editmain.tsx,App.tsxor the stylesheet, but they are worth reading. In the Terminal, start the dev server:npm run devOpen the Web Browser tab. At the top of the page, a banner reads "TypeScript is running: this line was changed by src/main.tsx." The HTML in
index.htmlsays "TypeScript has not run yet.", so seeing the new text confirms that a script changed the page. Below it you should see the Standup Agenda heading, an intro sentence and an Add topic button above an empty-looking list. The three seeded topics are bare<li>elements with nothing inside them, becausecreateAgendaItemis still a stub. Confirming the banner before you write anything shows that the dev server and your TypeScript are wired to the page.Press Ctrl+C in the Terminal to stop the server. You'll start it again in Step 6, when the board has something to show. ### Check Your Work
Each task is graded automatically when you complete it, so you get feedback as you go. You can also run the checks yourself in the Terminal whenever you like. Run one task's test with its name in quotes (this type-checks the project first):
bash runTest.sh "task 2.1"Run the whole suite:
npx --no-install vitest run --reporter=dotType-check without running any tests:
npm run typecheckEach 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. Everything you write lives in four files under
src/, one per layer of the board:tree.tswalks the tree,query.tsfinds and narrows elements,events.tswires clicks and keystrokes, andcomponents.tsbuilds and updates items. Steps 2 to 5 each end with graded tasks, and Step 6 runs the whole application. Step 2 starts withtree.ts.info> If you get stuck, you can refer to the provided solution code for each task, available in the
solutionfolder. -
Challenge
Step 2: Walk the Agenda Tree and Tell Nodes Apart
Walk the Agenda Tree and Tell Nodes Apart
Every agenda item in the DOM is a live tree: an
<li>holding a label, two buttons, an input, and a nested<ul>of its own sub-items, each of which can nest further. Before you can add or remove anything, the board needs helpers that can walk that tree correctly.A
Nodein the DOM is broader than you might expect: whitespace between tags becomes a text node, comments become comment nodes, and both show up innode.childNodesright alongside the actual<li>and<button>elements.node.children, by contrast, only contains elements, but it is not available on everyNode. A helper that accepts a genericNodehas to filterchildNodesitself rather than reach forchildren.You can check each task as you finish it with the commands from Step 1. With element-only children sorted out, the board also needs to go the other direction: given some element buried inside an item, such as a button, a span or an input, find the
<li class="agenda-item">that encloses it.parentNodewill happily hand you back aDocumentor other non-element ancestor once you climb high enough, butparentElementbecomesnullat exactly the point there's no more enclosing element, which makes it the safer property to loop on.childrenandchildNodesanswer different questions, and the board sometimes needs both answers at once. To see whatchildrenleaves out, count the elements fromchildrenand count the text and comment nodes fromchildNodes, using each node'snodeTypeto tell the kinds apart. -
Challenge
Step 3: Query and Narrow Agenda Elements
Query and Narrow Agenda Elements
With tree-walking helpers in place, the board's other modules need a fast way to find specific elements, whether every agenda item or one item's buttons and input, without re-implementing a tree walk each time.
querySelectorandquerySelectorAlldo this searching for you, but only within whatever element you call them on: calling them ondocumentsearches the entire page, while calling them on a specific item scopes the search to just that item's own subtree.There's a second wrinkle: both methods return results typed as
Element(ornull), which is deliberately vague. TypeScript has no way to know from a CSS selector string what tag ends up matching. Every query helper in this lab has to narrow that generic result to the concrete HTML interface the markup guarantees, whether that'sHTMLLIElement,HTMLButtonElement, orHTMLInputElement, before handing it back to a caller that wants tag-specific properties like.valueor.disabled. Item-level controls need the same scoped, narrowed treatment. CallingquerySelectoron an item rather thandocumentguarantees you get that item's own button, never a button belonging to a sibling or a nested sub-item that happens to share the same class. Narrowing doesn't always have to stop at anasassertion.instanceofchecks against a DOM interface likeHTMLInputElementat runtime, not just at the type level, which matters here because the same CSS class could in principle be applied to the wrong tag by a future markup change.instanceofcatches that mistake and an assertion would silently hide it. -
Challenge
Step 4: Add and Remove Nested Agenda Items with Events
Add and Remove Nested Agenda Items with Events
The board can now find any item and any of its controls. What's still missing is the response to a click or a keystroke, the part that actually grows or shrinks the tree.
Every one of these handlers follows the same shape: find the element that triggers the behavior, attach a listener for the relevant event, and inside that listener act on the right part of the tree. New items come from
createAgendaItem, whichevents.tsalready imports. Until you build it in Step 5 it returns a bare<li>, so the items you add in this step have no label or buttons yet, and the tasks only check that each item lands in the right list. Getting the scope of each query right, the item itself versus the whole document, is what keeps a click on one item's button from disturbing a sibling item elsewhere in the tree. Keyboard interaction works differently from a click: there's no dedicated element to listen on beyond the input you are already typing into, and the listener has to inspect which key was pressed before deciding to act, since every other keystroke should behave exactly as a plain text input normally does. Removing an item is the simplest of the three interactions, because the DOM does the hard part: detaching an element from its parent automatically detaches everything nested beneath it, so there's no need to separately walk down and remove sub-items one by one. So far every new item has gone to the end of a list, which is only one choice. Sometimes a new item belongs at the top or between two others.insertBeforeplaces a node ahead of a reference sibling you choose, and passingnullas the reference adds the node last. The Add topic button on the board uses this to put each new topic at the top. -
Challenge
Step 5: Compose Reusable Agenda Item Components
Compose Reusable Agenda Item Components
In Step 4 the handlers already ask
createAgendaItemfor each new item, but it still returns a bare<li>. This step builds it for real and adds two small helpers that update an item in place, so the rest of the board, and any future feature, can call them by name instead of duplicating markup.A component factory in this style takes the data an item needs (its label text, for instance) and returns a ready-to-attach DOM fragment, built with the exact class names the query helpers from Step 3 expect. Keeping those class names in one place means a future markup change only has to happen once. Once an item exists, something also needs to update it in place: renaming a topic without discarding its buttons, input, or nested sub-items. That means changing only the one child that holds the label, never the item's own content wholesale. The last piece of interactive state an item can carry is whether it's done.
classListoffersaddandremovefor one-directional changes, buttoggleis the one built for exactly this back-and-forth case, and it conveniently reports back which state the element ended up in. The board calls it when you click a topic's label, so you will see the done style in Step 6. -
Challenge
Step 6: Run the Application
Run 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. That is the one place the pieces you wrote separately become a single working program.
Run the Checks Again
Step 1 showed how to check your work. Run the whole suite once more to see every task pass together. In the Terminal:
npx --no-install vitest run --reporter=dot ``` Before you start the application, try one thing: break something on purpose, such as changing a return type in `src/query.ts`, 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. ### Start the Application In the **Terminal**, start the dev server: ```bash npm run devSwitch to the Web Browser tab. The board now shows three topics (Sprint review, Blockers and Announcements), each with a label, Add and Remove buttons and a text input. Click Add on a topic and a new sub-item nests under it. Type a label into an input and press Enter to nest a sub-item under that topic and clear the field. Click Remove to delete a topic along with everything nested under it. Click a label to mark that item done and click it again to reopen it. Click Add topic to add a new top-level topic, which lands at the top of the board. The page is running the functions you wrote, so what you see is your own code.
When you are done, press Ctrl+C in the Terminal to stop the server. Congratulations on finishing the lab. You built the TypeScript layer for a standup agenda board: helpers that walk the DOM tree, scoped queries narrowed to
HTMLLIElement,HTMLButtonElementandHTMLInputElement, click and Enter handlers that add, insert and remove nested items, a factory that builds each item, and a helper that marks one done.The same moves apply to any page you script in TypeScript: tell element nodes from other kinds of node, narrow what the query methods return instead of asserting it away, and keep the class names that tie markup to code in one place. Because the project type-checks under
strict, the compiler checked each of those helpers as well as the tests.
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.