Welcome! Today, we are going to explore an engaging task that involves managing employee records within a company. Specifically, we will work with nested objects and arrays to add projects and tasks for employees and retrieve those tasks as needed. This exercise will help you understand how to manipulate nested data structures efficiently using TypeScript's type safety features.
Let's start by discussing the methods we will implement in our EmployeeRecords class, utilizing type annotations for method parameters and return types to provide type safety.
addProject(employeeId: string, projectName: string): boolean— This method adds a new project to an employee's list of projects. If the project already exists for that employee, the method returnsfalse. Otherwise, it adds the project and returnstrue.addTask(employeeId: string, projectName: string, task: string): boolean— This method adds a new task to a specified project for an employee. If the project does not exist for that employee, the method returnsfalse. If the task is added successfully, it returnstrue.getTasks(employeeId: string, projectName: string): string[] | null— This method retrieves all tasks for a specified project of an employee. If the project does not exist for that employee, the method returnsnull. Otherwise, it returns the list of tasks.
Now, let's build our EmployeeRecords class step by step, ensuring we understand each component clearly.
In this initial setup, we define the EmployeeRecords class with a records object. The type ProjectTasks represents an object where project names are the keys and arrays of tasks are the values. EmployeeRecordsStorage maps employee IDs to their projects and tasks, ensuring flexible and type-safe management of employee records. Each employee ID key corresponds to an object that holds projects and their associated tasks, using TypeScript index signatures.
