Hamburger Icon
  • Labs icon Lab
  • Core Tech
Labs

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.

Labs

Path Info

Level
Clock icon Beginner
Duration
Clock icon 50m
Published
Clock icon Aug 15, 2023

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. 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.

  2. 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 and include which will dictate which files/directories should be compiled.

  3. Challenge

    Step 2: Implementing SquareShape

    Now you can start your implementation for some geometry calculations. In the SquareShape.ts file there is a SquareShape class defined. It has the export keyword which allows it to be used in other files, mainly index.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.

  4. Challenge

    Step 3: Implementing RoundShape

    Next is to do the same calculations for round shapes. In the RoundShape.ts file there is a RoundShape class defined. Like SquareShape, it also has the export keyword. You will be implementing the same functions here but for circles and spheres.

  5. Challenge

    Step 4: Exporting and Importing

    By prefixing the RoundShape and SquareShape classes with export, you can now import them in other files with a statement like import { 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. The src/index.ts file can be used to trivialize this by having all the import statements for your library classes in index and then re-exporting them using an export statement. This allows you to only need a single import statement in other files and modules where you can select the classes you need straight from index. 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.

George is a Pluralsight Author working on content for Hands-On Experiences. He is experienced in the Python, JavaScript, Java, and most recently Rust domains.

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.