Putting It All Together
Part of the Introduction To TypeScript section of Coddy's JavaScript journey — lesson 50 of 73.
Challenge
EasyYou are provided with the following from the previous challenge:
- The
Taskinterface withid(number),title(string), andstatus(literal type) - Three task variables:
firstTask,secondTask, andthirdTask - The
getTaskInfofunction - The
addTaskfunction - The
changeTaskStatusfunction - The
listTasksByStatusfunction - The
printTaskSummaryfunction - The
printAllTaskSummariesfunction - All task arrays:
initialTasks,updatedTasks,testTasks,progressTasks,completedTasks,mixedTasks,todoTasks,inProgressTasks,doneTasks, andsampleTasks
Create a comprehensive task management workflow that demonstrates all the functionality you've built:
Create a variable named projectTasks of type Task[] containing these initial tasks:
- A task with id
201, title"Setup development environment", and status'done' - A task with id
202, title"Create project structure", and status'todo'
Perform the following operations in sequence:
- Use
addTaskto add a new task with title"Write documentation"toprojectTasksand store the result inexpandedTasks - Use
changeTaskStatusto change the status of task with ID202to'in-progress'inexpandedTasksand store the result inupdatedProjectTasks - Use
changeTaskStatusto change the status of the newly added task (ID3) to'done'inupdatedProjectTasksand store the result infinalTasks - Use
listTasksByStatusto filterfinalTasksfor'done'status and store the result incompletedProjectTasks
Print the following outputs on separate lines:
- Print the length of
projectTasks - Print the length of
finalTasks - Print the length of
completedProjectTasks - Call
printTaskSummarywith the first task fromcompletedProjectTasks - Call
printTaskSummarywith the last task fromcompletedProjectTasks - Call
printAllTaskSummarieswithfinalTasks
Try it yourself
// Task interface and existing code from previous challenges
interface Task {
id: number;
title: string;
status: 'todo' | 'in-progress' | 'done';
}
const firstTask: Task = {
id: 1,
title: "Learn TypeScript interfaces",
status: "todo"
};
const secondTask: Task = {
id: 2,
title: "Build task management app",
status: "in-progress"
};
const thirdTask: Task = {
id: 3,
title: "Write unit tests",
status: "done"
};
function getTaskInfo(task: Task): string {
return `Task ${task.id}: ${task.title} - ${task.status}`;
}
function addTask(taskList: Task[], title: string): Task[] {
const newTask: Task = {
id: taskList.length + 1,
title: title,
status: 'todo'
};
return [...taskList, newTask];
}
function changeTaskStatus(taskList: Task[], taskId: number, newStatus: 'todo' | 'in-progress' | 'done'): Task[] {
return taskList.map(task =>
task.id === taskId ? { ...task, status: newStatus } : task
);
}
function listTasksByStatus(tasks: Task[], status: 'todo' | 'in-progress' | 'done'): Task[] {
return tasks.filter(task => task.status === status);
}
const initialTasks: Task[] = [firstTask, secondTask];
const updatedTasks: Task[] = addTask(initialTasks, "Review code changes");
const testTasks: Task[] = [firstTask, secondTask, thirdTask];
const progressTasks = changeTaskStatus(testTasks, 1, 'in-progress');
const completedTasks = changeTaskStatus(progressTasks, 2, 'done');
const mixedTasks: Task[] = [firstTask, secondTask, thirdTask];
const todoTasks = listTasksByStatus(mixedTasks, 'todo');
const inProgressTasks = listTasksByStatus(mixedTasks, 'in-progress');
const doneTasks = listTasksByStatus(mixedTasks, 'done');
function printTaskSummary(task: Task): void {
console.log(`ID: ${task.id}, Title: ${task.title}, Status: ${task.status}`);
}
function printAllTaskSummaries(taskList: Task[]): void {
for (let task of taskList) {
printTaskSummary(task);
}
}
const sampleTasks: Task[] = [
{ id: 101, title: "Design user interface", status: 'todo' },
{ id: 102, title: "Implement authentication", status: 'in-progress' },
{ id: 103, title: "Deploy to production", status: 'done' }
];
printTaskSummary(firstTask);
printTaskSummary(sampleTasks[1]);
printAllTaskSummaries(sampleTasks);
printTaskSummary(doneTasks[doneTasks.length - 1]);All lessons in Introduction To TypeScript
1Getting Started with TS
What is TypeScript?Why Use TypeScript?Your First TypeScript CodeCompilation Process & ErrorsRecap: Introduction to TS4Working with Functions
Typing Params & Return ValuesTyping Arrow FunctionsThe 'void' Return TypeOptional Parameters with '?'Default Parameter ValuesTyping Rest ParametersDefining Function TypesRecap: Building Typed Funcs7Project: A Simple Task List
Project: Defining Task StructFunction to Add a Task2Core Types
Basic Types: str, num, booleanThe 'any' Type: Escape HatchThe 'unknown' TypeWorking with 'null' & 'undef'Type Inference in ActionExplicit Type AnnotationsRecap: Core Types Practice5Types: Aliases, Unions & Inter
Type Aliases for PrimitivesUnion Types ('|')Working with Union TypesLiteral TypesIntersection Types ('&')Combining Type AliasesRecap: Advanced Type Combos3Data Structure: Arrays & Tuple
Typed Arrays'readonly' Modifier for ArraysWhat is a Tuple?Declaring and Accessing TuplesDestructuring TuplesReadonly TuplesMulti-dimensional Typed Arrays Spread Operator with ArraysRecap: Arrays and Tuples