Featured resource
2026 Tech Forecast
2026 Tech Forecast

1,500+ tech insiders, business leaders, and Pluralsight Authors share their predictions on what’s shifting fastest and how to stay ahead.

Download the forecast
  • Lab
    • Libraries: If you want this lab, consider one of these libraries.
    • Core Tech
Labs

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 platform
Lab Info
Level
Intermediate
Last updated
Nov 26, 2026
Duration
45m

Contact sales

By clicking submit, you agree to our Privacy Policy and Terms of Use, and consent to receive marketing emails from Pluralsight.
Table of Contents
  1. 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.html holds only a <div id="root"> mount point, and src/main.tsx mounts the App component from src/App.tsx. Before it does, main.tsx also makes one plain DOM change of its own, which you will check below. App renders 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, attachEnterKeyHandler and getTextInput. It also calls insertItemAt, findEnclosingAgendaItem and toggleDoneState when you use the board. src/styles.css already styles the classes the board uses, such as .agenda-item, .sub-items and .agenda-item-done, so the result of your code shows without any CSS from you. You won't edit main.tsx, App.tsx or the stylesheet, but they are worth reading. In the Terminal, start the dev server:

    npm run dev
    

    Open 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.html says "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, because createAgendaItem is 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=dot
    

    Type-check without running any tests:

    npm run typecheck
    

    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. Everything you write lives in four files under src/, one per layer of the board: tree.ts walks the tree, query.ts finds and narrows elements, events.ts wires clicks and keystrokes, and components.ts builds and updates items. Steps 2 to 5 each end with graded tasks, and Step 6 runs the whole application. Step 2 starts with tree.ts.

    info> If you get stuck, you can refer to the provided solution code for each task, available in the solution folder.

  2. 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 Node in the DOM is broader than you might expect: whitespace between tags becomes a text node, comments become comment nodes, and both show up in node.childNodes right alongside the actual <li> and <button> elements. node.children, by contrast, only contains elements, but it is not available on every Node. A helper that accepts a generic Node has to filter childNodes itself rather than reach for children.

    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. parentNode will happily hand you back a Document or other non-element ancestor once you climb high enough, but parentElement becomes null at exactly the point there's no more enclosing element, which makes it the safer property to loop on. children and childNodes answer different questions, and the board sometimes needs both answers at once. To see what children leaves out, count the elements from children and count the text and comment nodes from childNodes, using each node's nodeType to tell the kinds apart.

  3. 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. querySelector and querySelectorAll do this searching for you, but only within whatever element you call them on: calling them on document searches 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 (or null), 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's HTMLLIElement, HTMLButtonElement, or HTMLInputElement, before handing it back to a caller that wants tag-specific properties like .value or .disabled. Item-level controls need the same scoped, narrowed treatment. Calling querySelector on an item rather than document guarantees 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 an as assertion. instanceof checks against a DOM interface like HTMLInputElement at 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. instanceof catches that mistake and an assertion would silently hide it.

  4. 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, which events.ts already 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. insertBefore places a node ahead of a reference sibling you choose, and passing null as the reference adds the node last. The Add topic button on the board uses this to put each new topic at the top.

  5. Challenge

    Step 5: Compose Reusable Agenda Item Components

    Compose Reusable Agenda Item Components

    In Step 4 the handlers already ask createAgendaItem for 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. classList offers add and remove for one-directional changes, but toggle is 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.

  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 dev
    

    Switch 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, HTMLButtonElement and HTMLInputElement, 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

Pluralsight’s AI authoring technology is designed to accelerate the creation of hands-on, technical learning experiences. Serving as a first-pass content generator, it produces structured lab drafts aligned to learning objectives defined by Pluralsight’s Curriculum team. Each lab is then enhanced by our Content team, who configure the environments, refine instructions, and conduct rigorous technical and quality reviews. The result is a collaboration between artificial intelligence and human expertise, where AI supports scale and efficiency, and Pluralsight experts ensure accuracy, relevance, and instructional quality, helping learners build practical skills with confidence.

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.

Get started with Pluralsight