Essential Types And Notations 20 exercises
Problem

A Single Source of Truth for Type Definitions

Here's some code that uses the same type in multiple places:


const getRectangleArea = (rectangle: { width: number; height: number }) => {
return rectangle.width * rectangle.height;
};
const getRectanglePerimeter = (rectangle: {
width: number;
height: number;
}) => {
return 2 * (r

Loading exercise

Transcript

00:00 Okay, we've got some code here that uses like lots of the same type and we want to see if we can refactor it to make it a bit cleaner. We've got a getRectangleArea function which takes in a rectangle which has a width number and height number inside an object.

00:15 Down here we have getRectanglePerimeter which takes in a rectangle which again has exactly the same type as the one above. And ideally we would have it so this was a single source of truth because if we had to add something extra to the rectangle,

00:30 let's say color or something, then we wouldn't want to have to kind of go through all of the places that we've got our rectangles and edit each individual type. So it would be great if we could extract it out somewhere to a single source of truth.

00:43 So that is your job to try to find a specific keyword that will help you refactor this into its own type. Good luck.