Introduction

Welcome! Today, we will explore creating a simple address book application using TypeScript's Map. This task will help you understand how to manipulate Map in TypeScript, focusing on adding, retrieving, and deleting entries. By the end of this lesson, you'll have a solid grasp of these fundamental operations while ensuring data integrity through the type annotations that TypeScript provides.

Introducing Methods to Implement

In this task, we will implement three methods to manage our address book, using type annotations to ensure code reliability:

  • addContact(name: string, phoneNumber: string): boolean: Adds a new contact. Returns false if the contact already exists; otherwise, it adds the contact and returns true. In this task, let's assume phone numbers do not change, so it's not allowed to overwrite the existing contact's number.
  • getContact(name: string): string | undefined: Retrieves the phone number for a given name. Returns undefined if the contact does not exist.
  • deleteContact(name: string): boolean: Deletes a contact with the given name. Returns true if the contact exists and is deleted, false otherwise.

Let's break down each method in detail in the next sections.

Step 1: Initial Setup

Before we begin implementing the methods in our address book, we need to set up our TypeScript class that will hold the contacts using a Map.

Here's how to set up the initial structure:

class AddressBook {
    contacts: Map<string, string>;

    constructor() {
        this.contacts = new Map<string, string>();
    }
}

In this initial setup:

  • We define the AddressBook class with a contacts property, which is a Map that holds the name of the contact as the key and the phone number as the value.
  • We initialize the contacts property in the constructor, ensuring each instance of AddressBook starts with an empty map.
Step 2: Implementing 'addContact'

This method adds a new contact to the address book with the given name and phoneNumber. If the contact already exists, it returns false. Otherwise, it adds the contact and returns true.

Question: Why do you think we need to check if the contact already exists?

Answer: To avoid duplicating existing entries. Also, if a contact with the same name already exists, we shouldn't allow overwriting its phone number in this method, as it's only for creation.

Here is the method implementation:

addContact(name: string, phoneNumber: string): boolean {
    if (this.contacts.has(name)) {
        return false;
    }
    this.contacts.set(name, phoneNumber);
    return true;
}

In this method:

  • We use type annotations such as name: string and phoneNumber: string to ensure that inputs adhere to specified types.
  • We verify if the contact already exists using if (this.contacts.has(name)).
  • If it exists, we return false.
  • If it doesn't exist, we add it to our Map and return true.
Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal