Introduction

Hello, Space Explorer! Today, we’re going to explore an important concept in TypeScript: managing data using arrays and objects with the added power of type safety. To practice this concept, we will build a simple Student Management System. Specifically, we will create a class that stores students and their grades. This hands-on approach will help us understand how arrays and objects can be efficiently utilized with TypeScript's type system to enhance code safety and predictability. Ready to take on this task? Awesome, let's get started!

Introducing Methods to Implement

Our task involves implementing three primary methods within our class:

  1. addStudent(name: string, grade: number): void: Allows us to add a new student and their grade to our list. If the student is already on the list, their grade will be updated.
  2. getGrade(name: string): number | null: Retrieves the grade for a student given their name. Returns null if the student is not found.
  3. removeStudent(name: string): boolean: Removes a student from the list by their name. Returns true if the operation is successful and false if the student is not found.

Understanding the input and output types not only assists in code clarity but also prevents common runtime errors. Let’s break it down step-by-step.

Step 1: Setting up the Class

Let’s start by defining our StudentManager class, using TypeScript's array of typed objects to manage students and their grades.

type Student = {
    name: string;
    grade: number;
};

class StudentManager {
    students: Student[];

    constructor() {
        this.students = [];
    }
}

This initial code snippet sets up the foundation for our Student Management System. We define a Student type that outlines the structure of our student objects, with name and grade properties. The StudentManager class is then introduced with a students property, which is an array of Student objects. The constructor initializes this array as empty, ready to store the student data. This setup allows us to manage student information efficiently.

Step 2: Implementing 'addStudent'

The addStudent method ensures the correct types for the student's name and grade and maintains consistent student object types within the array. This is easily achieved thanks to TypeScript's type system, which enforces type safety and consistency in our code. The method adds a new student to the list or updates the grade of an existing student if the name is already present.

addStudent(name: string, grade: number): void {
    for (const student of this.students) {
        if (student.name === name) {
            student.grade = grade;
            return;
        }
    }
    this.students.push({ name, grade });
}

Let's break it down:

  • Using a for loop, we iterate through this.students.
  • If we find an object where the name property matches the given name, we update the grade.
  • If not found, we append a new object { name: name, grade: grade } to our list.

Why check for existing entries? To ensure only unique student names are stored, preserving data consistency.

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