- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Core Tech

Guided: Foundations in TypeScript
This lab has been crafted to provide hands-on experience with TypeScript through the creation of a straightforward currency converter application. You'll be building this app in incremental steps, each with specific tasks outlined in comprehensive guidelines. As you progress, you'll grasp fundamental TypeScript concepts such as types, inferences, and classes, alongside practical skills like DOM manipulation, making HTTP requests for API calls, managing asynchronous operations, and error handling. Upon completion, you will have demonstrated your ability by constructing a fully operational currency converter where you will input an amount, select currencies for conversion, and observe the conversion to the chosen target currency.

Lab Info
Table of Contents
-
Challenge
Introduction to the Currency Converter App
Welcome to this guided Code Lab where you’ll get some hands-on practice with TypeScript while building a simple Currency Converter application!
This application allows the user to enter an amount, select a from currency and a to currency and then get the amount converted to the target currency. You’ll build the application step by step, completing specific coding tasks with detailed instructions.
While working your way through this application, you’ll learn the basic TypeScript syntax including types, inferences, and classes. You'll also practice DOM Manipulation, how to make an API call using an HTTP request, asynchronous programming, and error handling.
After you complete a task, click the Validate button to check your work. If you get stuck or want to compare solutions, you can refer to the solution directory, which contains the completed project files.
After completing a step, you can build and run the application by doing the following:
- In the Terminal tab, run
npm run build
to build the application.- This creates the transpiled
.js
files inside thesrc/dist
directory.
- This creates the transpiled
- Run
npm run start
to start the application.- The Terminal shows a URL such as
http://127.0.0.1:8000/
. - Click the link to open the application in a new browser tab, or navigate using: {{localhost:8000}}
- The Terminal shows a URL such as
If you want to preview the application before completing any tasks, you can still build and run it using the commands above. You’ll see a page with two empty dropdowns, an input box for the amount, and an output box for the converted value.
- In the Terminal tab, run
-
Challenge
Set up
To start off, you need to set up the
Currency
class to hold the currency codes and names that you'll be using to populate the from and to currency dropdown boxes in the HTML form.You also need to set up the
Result
class that would hold the result of converting a given amount from the source currency to the target currency. Thereafter, you are required to read the available currencies from a JSON object and load them to the HTML form. -
Challenge
Making a HTTP GET Request to Fetch Exchange Rate Data
In this step, you'll first define an asynchronous function that takes in the base currency as a
string
parameter.Then, implement the function so that it makes an HTTP GET request using the JavaScript Fetch API (
fetch()
) to fetch the exchange rates for a given base currency for an external API. -
Challenge
Implementing the Currency Conversion Logic
In this step, you'll implement the
convertCurrency()
method of theUtil
class. This method will callfetchForexRates()
function, retrieve the exchange rate for theto
currency from the result, and then perform the currency conversion on the amount using the obtained value. -
Challenge
Bind Functionality to UI
In this step, you'll bind the functionality to the UI by implementing the
handleInput()
function to read input values and call theconvertCurrency()
method of theUtil
class.Then, you'll populate the resulting value in the UI and bind the event-handling logic to the event listener of the input event so everything works end to end.
-
Challenge
Fine Tuning
In this step, you'll perform error handling and formatting to improve the application.
You will:
- Add error handling for failed HTTP requests in the
fetchForexRates()
function, - Add error handing for invalid user input.
- Implement the
formatCurrency()
function and use it to format the converted amount in the target currency. You've now completed the Currency Converter in TypeScript!
Run the build command
npm run build
then the start commandnpm run start
in the Terminal tab. Launch the application in the browser as instructed in Step 1, and you should see the fully functional Currency Converter in action.When you enter a value in the Amount input box and select from and to currencies, the application displays the converted amount in the box below them, formatted in the target currency.
- Add error handling for failed HTTP requests in the
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.