Task model
- TypeScript course
- Beginner
- Lesson 76/89
- 5 min
Design task and status types.
Todo model
We define two core types for the Todo CLI. Status is a literal union representing a task's state: "active" or "completed". Todo represents a task with id, title, and status fields.
type Status = "active" | "completed";
interface Todo { id: string; title: string; status: Status; }Empty task list
An empty task list is an empty array of type Todo[]. This is the CLI's initial state; the array grows as you add tasks.
const todos: Todo[] = [];
console.log(todos.length); // 0Practice Write the Todo model
Define a Status type as "active" | "completed". Define a Todo interface: id: string, title: string, status: Status. Create an empty todos array and print its length.
Starter code
// Status türünü tanımla (iki değerli literal)
// Todo arayüzü: id, title ve status alanları
// todos dizisi oluştur (boş)
// uzunluğu yazdırSample solution
type Status = "active" | "completed";
interface Todo {
id: string;
title: string;
status: Status;
}
const todos: Todo[] = [];
console.log(todos.length);Expected output
0
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What type should a Todo's status field be?
- boolean
- "active" | "completed" Correct answer
- string
- number
Explanation: Status is a union of literal types: "active" or "completed". This guarantees only two valid states; string would allow any value.
2 Complete the Status union
type Status = "active" ____ "completed";
type Status = "active" | "completed";
Explanation: Status is a union of two literals with the pipe. & is intersection (would be never), || and + are not type operators.
3 Todo model parts
| Term | Meaning |
|---|---|
Status |
Status type (active or completed) |
Todo |
Task model (id, title, status) |
"active" | "completed" |
Two status literals |
Todo[] |
Task list |