Hamburger Icon
  • Labs icon Lab
  • Core Tech
Labs

Guided: Building a Geometry Helper Library with Typescript 5 - Part 2

This lab is the second in a 2-part series wherein you will be building a geometry helper library using Typescript. In this part, you will be using enums, interfaces, and type guards to implement functions and types that are needed to support 2D points. Once implemented, you will continue to utilize exports and imports for your library to be used in the example application.

Labs

Path Info

Level
Clock icon Beginner
Duration
Clock icon 46m
Published
Clock icon Aug 28, 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 - 2. This is the second lab in a 2-lab series in which you will be implementing a library for performing geometric calculations using Typescript.

    In this part of the series, you will be using structures such as enums, interfaces, and type guards to implement calculations between 2D points. 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: Implementing Types

    First, you will need to define a few structures for handling the calculations between 2D points.

    A LineType enum, or set of constant values, would be useful here for determining the type of the line formed between two points. A Point interface would also be necessary here to specify the structure of a 2D point. In this case, it would just be an object with x and y properties as numbers.

    Lastly, a type guard would be useful to verify that a specified object correctly implements the Point interface. In other words, verify that an object is indeed a Point. This will be necessary for validating inputs in the main app.ts file.

  3. Challenge

    Step 2: Implementing Point Calculations

    Now that you have defined LineType, Point, and the isPoint type guard, you will now need to implement functions for 2D point calculations. These include slope, distance, midpoint, and equation of a line passing through two given points.

    All of these functions will take two Point parameters, which allow any object that implements the Point interface. They will then return strings or numbers by calculating the specified value using the x and y coordinates of the two given Point parameters.

  4. Challenge

    Step 3: Exporting and Importing

    Lastly, you will need to import your types and calculation functions into index.ts and re-export them for use in app.ts just like you did in part 1 of this lab.

    This time, you will have many more types and functions prefaced with the export keyword in their individual files. This can make importing in index fairly verbose, so instead you can just import the file in index with a statement such as import filepath. Re-exporting everything from this import typically follows the syntax export * from filepath. All done! You can now run the application by clicking the Run button in the Terminal.

    When executed, the application will prompt you for the calculations you'd like to perform (including the shapes from part 1) before asking you to input your desired values. Once entered, it will display the results from the calculations using the functions you implemented!

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.