- Lab
- Core Tech

Guided: Building a Geometry Helper Library with Typescript 5 - Part 1
This lab is the first in a 2-part series wherein you will be building a geometry helper library using Typescript. In this part, you will be responsible for implementing functions that perform geometric calculations for various shapes. You will also be setting up a configuration for the project to use Typescript, along with utilizing exports and imports for your library to be used in an example application.

Path Info
Table of Contents
-
Challenge
Introduction
Welcome to Building a Geometry Helper Library with Typescript Part - 1. This is the first lab in a 2-lab series in which you will be implementing a library for performing geometric calculations using Typescript.
The project you have been given is a standard Node project. You will need to create and configure Typescript settings using a
tsconfig.json
file before implementing the library using Typescript paradigms. Lastly, you will be exporting and importing your library to be used within the main application. If you are stuck at any time, feel free to take a look at the solution directory.The application will throw errors when run until you complete the lab, wherein it should work correctly. It can be started by clicking the Run button in the Terminal or by running the command
npm start
in the Terminal. -
Challenge
Step 1: Setting up Typescript
The first thing you will need to do is setup a Typescript configuration. This is done by creating a
tsconfig.json
file. This file will contain two key properties:compilerOptions
which are settings for how Typescript files will be compiled andinclude
which will dictate which files/directories should be compiled. -
Challenge
Step 2: Implementing SquareShape
Now you can start your implementation for some geometry calculations. In the
SquareShape.ts
file there is aSquareShape
class defined. It has theexport
keyword which allows it to be used in other files, mainlyindex.ts
and by extension, the main application. Your task here is to calculate values such as area, perimeter, volume, and surface area for squares and cubes. -
Challenge
Step 3: Implementing RoundShape
Next is to do the same calculations for round shapes. In the
RoundShape.ts
file there is aRoundShape
class defined. LikeSquareShape
, it also has theexport
keyword. You will be implementing the same functions here but for circles and spheres. -
Challenge
Step 4: Exporting and Importing
By prefixing the
RoundShape
andSquareShape
classes withexport
, you can now import them in other files with a statement likeimport { RoundShape } from "./RoundShape"
. In this application context, importing two classes isn't that big of a deal. However, in a larger project you might have many more classes to use and having to copy the same import statements across multiple files and modules can become a hassle.This is where
index.ts
comes in. Thesrc/index.ts
file can be used to trivialize this by having all the import statements for your library classes inindex
and then re-exporting them using anexport
statement. This allows you to only need a singleimport
statement in other files and modules where you can select the classes you need straight fromindex
. Now that the library classes are implemented and correctly exported/imported into the main application, you can run the application to see the results. Running the application will print out four lines to the Terminal showing the geometric calculations for each shape with a specified value.
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.