Project: Defining Task Struct
Part of the Introduction To TypeScript section of Coddy's JavaScript journey — lesson 45 of 73.
Welcome to your first TypeScript project! Over the next few lessons, you'll build a complete task list application that demonstrates how TypeScript helps create well-structured, type-safe code. This project will combine many of the concepts you've learned so far: interfaces, literal types, arrays, and functions.
The foundation of any task management system is defining what a task looks like. In this lesson, you'll create the data structure that represents a single task in your application. This structure needs to capture the essential information: a unique identifier, what the task is about, and its current state.
Your Task interface will use literal types for the status property, which means the status can only be one of a few specific string values like 'todo', 'in-progress', or 'done'. This approach prevents typos and ensures consistency throughout your application - TypeScript will catch any attempt to use an invalid status value.
This project structure will serve as the blueprint for all the functionality you'll build in the upcoming lessons, including adding new tasks, updating their status, and filtering tasks by their current state.
Challenge
EasyCreate an interface named Task with the following properties:
idof typenumbertitleof typestringstatusthat can only be one of these literal values:'todo','in-progress', or'done'
Using your Task interface, create the following variables:
- Create a variable named
firstTaskof typeTaskwith id1, title"Learn TypeScript interfaces", and status'todo' - Create a variable named
secondTaskof typeTaskwith id2, title"Build task management app", and status'in-progress' - Create a variable named
thirdTaskof typeTaskwith id3, title"Write unit tests", and status'done'
Create a function named getTaskInfo that accepts a parameter of type Task and returns a string in the format "Task [id]: [title] - [status]".
Print the following outputs on separate lines:
- Call
getTaskInfowithfirstTaskand print the result - Call
getTaskInfowithsecondTaskand print the result - Call
getTaskInfowiththirdTaskand print the result - Print the status of
firstTask - Print the title of
secondTask
Try it yourself
// TODO: Write your code here
// Create the Task interface
// Create the task variables
// Create the getTaskInfo function
// Print the required outputsThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
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