- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Core Tech
Guided: Implement Interfaces, Enums, and Type Guards in TypeScript - Geometry Helper 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.
Lab Info
Table of Contents
-
Challenge
Introduction
Welcome to Guided: Implement Interfaces, Enums, and Type Guards in TypeScript - Geometry Helper Part 2. This is the second lab in a two-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 involving 2D points.
Finally, 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. Once completed, it should work correctly.
How to Run the App:
The application can be started by clicking the Run button in the Terminal or by running the command
npm startin the Terminal. -
Challenge
Step 1: Implementing Types
First, you will need to define a few structures for handling the calculations between 2D points.
A
LineTypeenum, or set of constant values, is useful here for determining the type of the line formed between two points. APointinterface is necessary here to specify the structure of a 2D point. In this case, it will just be an object withxandyproperties as numbers.Finally, a type guard is useful to verify that a specified object correctly implements the
Pointinterface. In other words, it will verify that an object is indeed aPoint. This will be necessary for validating inputs in the mainapp.tsfile. -
Challenge
Step 2: Implementing Point Calculations
Now that you've defined
LineType,Point, and theisPointtype guard, you'll now need to implement functions for 2D point calculations. These include slope, distance, midpoint, and the equation of a line passing through two given points.All of these functions will take two
Pointparameters, which allow any object that implements thePointinterface. They will then return astringornumberby calculating the specified value using thexandycoordinates of the two givenPointparameters. -
Challenge
Step 3: Exporting and Importing
Finally, you'll need to import your types and calculation functions into
index.tsand re-export them for use inapp.tsjust like you did in part 1 of this lab.This time, you'll have many more types and functions prefaced with the
exportkeyword in their individual files. This can make importing inindexfairly 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 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!
About the author
Real skill practice before real-world application
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.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.