- Lab
- Core Tech

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.

Path Info
Table of Contents
-
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. -
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. APoint
interface would also be necessary here to specify the structure of a 2D point. In this case, it would just be an object withx
andy
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 aPoint
. This will be necessary for validating inputs in the mainapp.ts
file. -
Challenge
Step 2: Implementing Point Calculations
Now that you have defined
LineType
,Point
, and theisPoint
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 thePoint
interface. They will then returnstring
s ornumber
s by calculating the specified value using thex
andy
coordinates of the two givenPoint
parameters. -
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 inapp.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 inindex
fairly verbose, so instead you can just import the file in index with a statement such asimport filepath
. Re-exporting everything from this import typically follows the syntaxexport * 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!
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.