- Lab
- Core Tech

Guided: Build a Currency Converter 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.

Path 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 to/target currency. You’ll be building this application in steps and in each step you’ll be completing various coding tasks. You are given detailed guidelines to complete those tasks.
While working your way through this application, you’ll learn the basic Typescript syntax including types, inferences & classes, DOM Manipulation, how to make an API call using an HTTP request, asynchronous programming and error handling.
After you complete each task, they can be validated by clicking on the Validate button in each task . If you are stuck or would like to compare solutions, a solution directory has been provided for you as well with the completed project files.
After completing a step, you can build and run the application by doing the following:
- To build the application, you can give the command
npm run build
in the Terminal tab of your lab environment. (This will create the transpiled.js
files inside thesrc/dist
directory ) - To run the application, you can give the command
npm run start
in the Terminal tab. After giving this command, you'll see a URL like this in your Terminal window:http://127.0.0.1:8000/
. Clicking on that link will open up the application on a new browser tab, or you can navigate there by clicking on the following link: {{localhost:8000}}
If you are curious to see what the application UI looks like, prior to attempting any tasks, you could still use the above 2 commands to build and run the application. You'll see a page with 2 empty dropdown boxes, an input box to enter the amount & an output box to display the converted amount.
- To build the application, you can give the command
-
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 theResult
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. Thereafter, you should implement the function so that it makes a HTTP GET request using the JavaScript Fetch API i.e. thefetch()
function, 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, to callfetchForexRates()
function, obtain the exchange rate for theto
currency, from the result of thefetchForexRates()
function call and then perform the currency conversion for the amount using the obtained value. -
Challenge
Bind functionality to UI
In this step, you'll be binding the functionality to the UI by implementing the
handleInput()
function to read the input values and call theconvertCurrency()
method of theUtil
class. Thereafter, populate the resulting value in the UI and then bind the event handling logic to the event listener of the input event of the form for everything to work. -
Challenge
Fine tuning
In this step, you'll mainly perform error handling and formatting. You'll be adding error handling for failed HTTP request in the
fetchForexRates()
function, and also for invalid user input. You'll also be implementing theformatCurrency()
function and use it to format the converted amount in the target currency. You are now done with implementing the Currency Converter in TypeScript! When you use the build commandnpm run build
then the start commandnpm run start
in the Terminal tab and launch the application in the browser as instructed in Step 1, you should see the fully functional Currency Converter in action.When you type a value in the Amount input box and select from and to currencies, you should see the converted amount in the box below them, formatted in the target currency.
What's a lab?
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.
Provided environment for hands-on practice
We will provide the credentials and environment necessary for you to practice right within your browser.
Guided walkthrough
Follow along with the author’s guided walkthrough and build something new in your provided environment!
Did you know?
On average, you retain 75% more of your learning if you get time for practice.