TypeScript Comprehensive Project Practice
This tutorial uses a complete project case to comprehensively apply the various features of TypeScript.
From project setup to actual development, it fully demonstrates the application of TypeScript in real projects.
Why a comprehensive project is needed
After learning TypeScript syntax, you need to consolidate your knowledge through real projects.
This tutorial presents a complete task management system covering the frontend, backend, and type definitions.
Through this project, you can master best practices for TypeScript in real development.
Project goals:Create a task management system with task creation, query, update, and delete functions.
Project Structure
Organize the project structure using a Monorepo style.
Directory structure
task
manager
/
├── src
/
│ ├── types
/
# Type definitions
│ │ ├── task.
ts
# Task types
│ │ ├── api.
ts
# API types
│ │ └── index.
ts
# Type exports
│ │
│ ├── services
/
# Service layer
│ │ ├── taskService.
ts
# Task service
│ │ └── index.
ts
│ │
│ ├── components
/
# React components
│ │ ├── TaskList.
tsx
# Task list
│ │ ├── TaskItem.
tsx
# Task item
│ │ ├── TaskForm.
tsx
# Task form
│ │ └── index.
ts
│ │
│ ├── hooks
/
# Custom Hooks
│ │ ├── useTasks.
ts
# Task state management
│ │ └── index.
ts
│ │
│ ├── App.
tsx
# Main application
│ ├── App.
css
# Styles
│ └── main.
tsx
# Entry file
│
├── index.
html
├── package.
json
├── tsconfig.
json
└── vite.
config
.
ts
Directory divisions:Divide directories by function, separating type definitions, the service layer, and components.
Type definitions
First, define the project's core types.
src/types/task.ts
// Task status enum
export
type TaskStatus
=
"pending"
|
"in-progress"
|
"completed"
;
// Task priority enum
export
type TaskPriority
=
"low"
|
"medium"
|
"high"
;
// Task interface definition
export
interface
Task
{
id
:
string
;
// Task ID
title
:
string
;
// Task title
description
?:
string
;
// Task description (optional)
status
:
TaskStatus
;
// Task status
priority
:
TaskPriority
;
// Task priority
createdAt
:
string
;
// Creation time
updatedAt
:
string
;
// Update time
dueDate
?:
string
;
// Due date (optional)
tags
?:
string
[
]
;
// Tags (optional)
}
// Input type for creating a task
export
interface
CreateTaskInput
{
title
:
string
;
description
?:
string
;
priority
:
TaskPriority
;
dueDate
?:
string
;
tags
?:
string
[
]
;
}
// Input type for updating a task
export
interface
UpdateTaskInput
{
title
?:
string
;
description
?:
string
;
status
?:
TaskStatus
;
priority
?:
TaskPriority
;
dueDate
?:
string
;
tags
?:
string
[
]
;
}
// Task filter options
export
interface
TaskFilter
{
status
?:
TaskStatus
;
priority
?:
TaskPriority
;
search
?:
string
;
}
Type layering:Define input types, output types, and filter types separately for easier maintenance.
API type definitions
Define types related to the API.
< h2 class="example">src/types/api.ts
// Common API response type
export
interface
ApiResponse
<
T
{
success:
boolean
;
data
?:
T
;
error
?:
string
;
message
?:
string
;
}
// Pagination metadata
export
interface
PaginationMeta
{
total
:
number
;
page
:
number
;
pageSize
:
number
;
totalPages
:
number
;
}
// Paginated response type
export
interface
PaginatedResponse
<
T
{
items:
T
[
]
;
meta
:
PaginationMeta
;
}
// Request error type
export
interface
ApiError
{
code
:
string
;
message
:
string
;
details
?:
Record
<
string
,
string;
}
// HTTP method types
export
type HttpMethod
=
"GET"
|
"POST"
|
"PUT"
|
"PATCH"
|
"DELETE"
;
// Task-related API endpoints
export
interface
TaskEndpoints
{
getAll
:
"/api/tasks"
;
getById
:
"/api/tasks/:id"
;
create
:
"/api/tasks"
;
update
:
"/api/tasks/:id"
;
delete
:
"/api/tasks/:id"
;
}
API types:A unified response format and error-handling types make frontend-backend integration easier.
Task service layer
Implements the business logic for task management.
src/services/taskService.ts
// Import type definitions
import
{
Task
,
CreateTaskInput
,
UpdateTaskInput
,
TaskFilter
,
TaskStatus
,
TaskPriority
}
from
"../types/task"
;
// Generate a unique ID
function
generateId
(
)
:
string
{
return
Date
.
now
(
)
.
toString
(
36
)
Math
.
random
(
)
.
toString
(
36
)
.
substr
(
2
)
;
}
// Mock database (in-memory storage)
let tasks
:
Task
[
]
=
[
{
id
:
"1"
,
title
:
"Learn TypeScript"
,
description
:
"Master TypeScript fundamentals and advanced features"
,
status
:
"completed"
,
priority
:
"high"
,
createdAt
:
new
Date
(
)
.
toISOString
(
)
,
updatedAt
:
new
Date
(
)
.
toISOString
(
)
,
tags
:
[
"Learning"
,
"TypeScript"
]
}
,
{
id
:
"2"
,
title
:
"Develop a task management system"
,
description
:
"Develop using React + TypeScript"
,
status
:
"in-progress"
,
priority
:
"high"
,
createdAt
:
new
Date
(
)
.
toISOString
(
)
,
updatedAt
:
new
Date
(
)
.
toISOString
(
)
,
tags
:
[
"Project"
,
"Practice"
]
}
]
;
// Task service class
class
TaskService
{
// Get all tasks
getAll
(
filter
?:
TaskFilter
)
:
Task
[
]
{
let result
=
[
...
tasks
]
;
if
(
filter
)
{
if
(
filter.
status
)
{
result
=
result.
filter
(
t
=>
t.
status
===
filter.
status
)
;
}
if
(
filter.
priority
)
{
result
=
result.
filter
(
t
=>
t.
priority
===
filter.
priority
)
;
}
if
(
filter.
search
)
{
const
search
=
filter.
search
.
toLowerCase
(
)
;
result
=
result.
filter
(
t
=>
t.
title
.
toLowerCase
(
)
.
includes
(
search
)
||
t.
description
?
.
toLowerCase
(
)
.
includes
(
search
)
)
;
}
}
return
result
;
}
// Get a task by ID
getById
(
id
:
string
)
:
Task
|
undefined
{
return
tasks.
find
(
t
=>
t.
id
===
id
)
;
}
// Create a task
create
(
input
:
CreateTaskInput
)
:
Task
{
const
now
=
new
Date
(
)
.
toISOString
(
)
;
const
task
:
Task
=
{
id
:
generateId
(
)
,
title
:
input.
title
,
description
:
input.
description
,
status
:
"pending"
,
priority
:
input.
priority
,
createdAt
:
now
,
updatedAt
:
now
,
dueDate
:
input.
dueDate
,
tags
:
input.
tags
}
;
tasks.
push
(
task
)
;
return
task
;
}
// Update a task
update
(
id
:
string
,
input
:
UpdateTaskInput
)
:
Task
|
null
{
const
index
=
tasks.
findIndex
(
t
=>
t.
id
===
id
)
;
if
(
index
===
1
)
return
null
;
const
task
=
tasks
[
index
]
;
const
updated
:
Task
=
{
...
task
,
...
input
,
updatedAt
:
new
Date
(
)
.
toISOString
(
)
}
;
tasks
[
index
]
=
updated
;
return
updated
;
}
// Delete a task
delete
(
id
:
string
)
:
boolean
{
const
index
=
tasks.
findIndex
(
t
=>
t.
id
===
id
)
;
if
(
index
===
1
)
return
false
;
tasks.
splice
(
index
,
1
)
;
return
true
;
}
// Update task status
updateStatus
(
id
:
string
,
status
:
TaskStatus
)
:
Task
|
null
{
return
this
.
update
(
id
,
{
status
}
)
;
}
}
// Export the service instance
export
const
taskService
=
new
TaskService
(
)
;
Service layer:Business logic is centralized in the service layer, making it easy to test and maintain.
Custom Hooks
Use Hooks to manage task state.
< h2 class="example">src/hooks/useTasks.ts
// Import React Hooks and types
import
{
useState
,
useEffect
,
useCallback
}
from
"react"
;
import
{
Task
,
CreateTaskInput
,
UpdateTaskInput
,
TaskFilter
,
TaskStatus
,
TaskPriority
}
from
"../types/task"
;
import
{
taskService
}
from
"../services/taskService"
;
// State type returned by the Hook
interface
UseTasksReturn
{
tasks
:
Task
[
]
;
loading
:
boolean
;
error
:
string
|
null
;
filter
:
TaskFilter
;
// Operation methods
createTask
:
(
input
:
CreateTaskInput
)
=>
Promise
<
void
;
updateTask
:
(
id
:
string
,
input
:
UpdateTaskInput
)
=>
Promise
<
void
;
deleteTask
:
(
id
:
string
)
=>
Promise
<
void
;
updateStatus
:
(
id
:
string
,
status
:
TaskStatus
)
=>
Promise
<
void
;
setFilter
:
(
filter
:
TaskFilter
)
=>
void
;
refresh
:
(
)
=>
void
;
}
// Initialize the default filter
const
defaultFilter
:
TaskFilter
=
{
}
;
export
function
useTasks
(
)
:
UseTasksReturn
{
// Task list state
const
[
tasks
,
setTasks
]
=
useState
<
Task
[
]
(
[
]
);
// Loading state
const
[
loading
,
setLoading
]
=
useState
(
true
)
;
// Error state
const
[
error
,
setError
]
=
useState
<
string
|
null
(
null
);
// Filter condition
const
[
filter
,
setFilter
]
=
useState
<
TaskFilter
(
defaultFilter
);
// Load the task list
const
loadTasks
=
useCallback
(
(
)
=>
{
setLoading
(
true
)
;
setError
(
null
)
;
try
{
const
data
=
taskService.
getAll
(
filter
)
;
setTasks
(
data
)
;
}
catch
(
err
)
{
setError
(
err
instanceof
Error
?
err.
message
:
"Load failed"
)
;
}
finally
{
setLoading
(
false
)
;
}
}
,
[
filter
]
)
;
// Reload on initial load and when the filter changes
useEffect
(
(
)
=>
{
loadTasks
(
)
;
}
,
[
loadTasks
]
)
;
// Create a task
const
createTask
=
useCallback
(
async
(
input
:
CreateTaskInput
)
=>
{
try
{
taskService.
create
(
input
)
;
loadTasks
(
)
;
}
catch
(
err
)
{
setError
(
err
instanceof
Error
?
err.
message
:
"Create failed"
)
;
}
}
,
[
loadTasks
]
)
;
// Update a task
const
updateTask
=
useCallback
(
async
(
id
:
string
,
input
:
UpdateTaskInput
)
=>
{
try
{
taskService.
update
(
id
,
input
)
;
loadTasks
(
)
;
}
catch
(
err
)
{
setError
(
err
instanceof
Error
?
err.
message
:
"Update failed"
)
;
}
}
,
[
loadTasks
]
)
;
// Delete a task
const
deleteTask
=
useCallback
(
async
(
id
:
string
)
=>
{
try
{
taskService.
delete
(
id
)
;
loadTasks
(
)
;
}
catch
(
err
)
{
setError
(
err
instanceof
Error
?
err.
message
:
"Delete failed"
)
;
}
}
,
[
loadTasks
]
)
;
// Update task status
const
updateStatus
=
useCallback
(
async
(
id
:
string
,
status
:
TaskStatus
)
=>
{
try
{
taskService.
updateStatus
(
id
,
status
)
;
loadTasks
(
)
;
}
catch
(
err
)
{
setError
(
err
instanceof
Error
?
err.
message
:
"Status update failed"
)
;
}
}
,
[
loadTasks
]
)
;
// Refresh the task list
const
refresh
=
useCallback
(
(
)
=>
{
loadTasks
(
)
;
}
,
[
loadTasks
]
)
;
return
{
tasks
,
loading
,
error
,
filter
,
createTask
,
updateTask
,
deleteTask
,
updateStatus
,
setFilter
,
refresh
}
;
}
Custom Hook:Encapsulate state management and business logic in a Hook, making components cleaner.
React components
Use Hooks to create a task list component.
src/components/TaskList.tsx
// Import React and the custom Hook
import
React from
"react"
;
import
{
useTasks
}
from
"../hooks/useTasks"
;
import
{
Task
,
TaskStatus
,
TaskPriority
}
from
"../types/task"
;
// Task item component Props
interface
TaskItemProps
{
task
:
Task
;
onStatusChange
:
(
id
:
string
,
status
:
TaskStatus
)
=>
void
;
onDelete
:
(
id
:
string
)
=>
void
;
}
// Task item component
const
TaskItem
:
React.
FC
<
TaskItemProps
=
(
{
task
,
onStatusChange
,
onDelete
}
)
=>
{// Status style mapping
const
statusStyles
:
Record
<
TaskStatus
,
string
=
{
"pending":
"status-pending"
,
"in-progress"
:
"status-progress"
,
"completed"
:
"status-completed"
}
;
// Priority style mapping
const
priorityLabels
:
Record
<
TaskPriority
,
string
=
{
"low":
"Low"
,
"medium"
:
"Center"
,
"high"
:
"High"
}
;
return
(
<
div className
=
{
`task
item $
{
statusStyles
[
task.
status
]
}
`
}
<
div className
=
"task-content"
<
h3 className
=
"task-title"
{
task.
title
}
h3
{
task.
description
&&
(
<
p className
=
"task-description"
{
task.
description
}
p
)
}
<
div className
=
"task-meta"
<
span className
=
{
`priority priority
$
{
task.
priority
}
`
}
{
priorityLabels
[
task.
priority
]
}
span
<
span className
=
"task-date"
{
new
Date
(
task.
createdAt
)
.
toLocaleDateString
(
)
}
span
div
div
<
div className
=
"task-actions"
<select value={>task.
status
}
onChange
=
{
(
e
)
=>
onStatusChange
(
task.
id
,
e.
target
.
value
as TaskStatus
)
}
className
=
"status-select"
<
option value
=
"pending"
Pending
option
<
option value
=
"in-progress"
In progress
option
<
option value
=
"completed"
Completed
option
select
<button onClick={()=>onDelete(>task.
id
)
}
className
=
"delete-btn"
Delete
button
div
div
)
;
}
;
// Task list component
export
const
TaskList
:
React.
FC
=
(
)
=>
{
// Use the custom Hook to get task state
const
{
tasks
,
loading
,
error
,
updateStatus
,
deleteTask
}
=
useTasks
(
)
;
// Render loading state
if
(
loading
)
{
return
<
div className
=
"loading"
Loading...
div
;
}
// Render error state
if
(
error
)
{
return
<
div className
=
"error"
Error
:
{
error
}
div
;
}
// Render empty state
if
(
tasks.
length
===
0
)
{
return
(
<
div className
=
"empty"
<
p
No tasks yet
p
<
p
Click the button above to create a new task
p
div
)
;
}
// Render the task list
return
(
<
div className
=
"task-list"
{
tasks.
map
(
task
=>
(
<
TaskItem
key
=
{task.
id
}
task
=
{
task
}
onStatusChange
=
{
updateStatus
}
onDelete
=
{
deleteTask
}
/>
)
)
}
div
)
;
}
;
export
default
TaskList
;
Component splitting:Split TaskItem into a standalone component for clearer code.
Main app component
Integrate all components into a complete application.
src/App.tsx
// Import React and types
import
React
,
{
useState
}
from
"react"
;
import
{
TaskList
}
from
"./components/TaskList"
;
import
{
useTasks
}
from
"./hooks/useTasks"
;
import
{
CreateTaskInput
,
TaskPriority
}
from
"./types/task"
;
import
"./App.css"
;
// Task form component Props
interface
TaskFormProps
{
onSubmit
:
(
input
:
CreateTaskInput
)
=>
void
;
}
// Task form component
const
TaskForm
:
React.
FC
<
TaskFormProps
=
(
{
onSubmit
}
)
=>
{// Form state
const
[
title
,
setTitle
]
=
useState
(
""
)
;
const
[
description
,
setDescription
]
=
useState
(
""
)
;
const
[
priority
,
setPriority
]
=
useState
<
TaskPriority
(
"medium"
);
// Submit handler
const
handleSubmit
=
(
e
:
React.
FormEvent
)
=>
{
e.
preventDefault
(
)
;
if
(
!
title.
trim
(
)
)
{
alert
(
"Please enter the task title"
)
;
return
;
}
onSubmit
(
{
title
:
title.
trim
(
)
,
description
:
description.
trim
(
)
||
undefined
,
priority
}
)
;
// Reset the form
setTitle
(
""
)
;
setDescription
(
""
)
;
setPriority
(
"medium"
)
;
}
;
return
(
<
form onSubmit
=
{
handleSubmit
}
className
=
"task-form"
<input type="text" value={title}onChange={(e)=>setTitle(>e.
target
.
value
)
}
placeholder
=
"Enter the task title"
className
=
"form-input"
/>
<input type="text" value={description}onChange={(e)=>setDescription(>e.
target
.
value
)
}
placeholder
=
"Enter the task description (optional)"
className
=
"form-input"
/>
<select value={priority}onChange={(e)=>setPriority(>e.
target
.
value
as TaskPriority
)
}
className
=
"form-select"
<
option value
=
"low"
Low priority
option
<
option value
=
"medium"
Medium priority
option
<
option value
=
"high"
High priority
option
select
<
button type
=
"submit"
className
=
"submit-btn"
Add task
button
form
)
;
}
;
// Main app component
const
App
:
React.
FC
=
(
)
=>
{
// Use the custom Hook
const
{
createTask
,
error
}
=
useTasks
(
)
;
// Render error message
const
renderError
=
(
)
=>
{
if
(
!
error
)
return
null
;
return
<
div className
=
"app-error"
{
error
}
div
;
}
;
return
(
<
div className
=
"app"
<
header className
=
"app-header"
<
h1
TypeScript Task Management System
h1
<
p
Comprehensive hands-on project
p
header
{
renderError
(
)
}
<
main className
=
"app-main"
<
TaskForm onSubmit
=
{
createTask
}
/>
<
TaskList
/>
main
<footer className
=
"app-footer"
<
p
Powered by TypeScript
React
p
footer
div
)
;
}
;
export
default
App
;
Component composition:Build complex UIs by composing simple components.
Project configuration
The project's TypeScript and build configuration.
tsconfig.json
{
"compilerOptions"
:
{
"target"
:
"ES2020"
,
"useDefineForClassFields"
:
true
,
"lib"
:
[
"ES2020"
,
"DOM"
,
"DOM.Iterable"
]
,
"module"
:
"ESNext"
,
"skipLibCheck"
:
true
,
"moduleResolution"
:
"bundler"
,
"allowImportingTsExtensions"
:
true
,
"resolveJsonModule"
:
true
,
"isolatedModules"
:
true
,
"noEmit"
:
true
,
"jsx"
:
"react-jsx"
,
"strict"
:
true
,
"noUnusedLocals"
:
true
,
"noUnusedParameters"
:
true
,
"noFallthroughCasesInSwitch"
:
true
}
,
"include"
:
[
"src"
]
,
"references"
:
[
{
"path"
:
"./tsconfig.node.json"
}
]
}
Strict mode:Enable strict for the most complete type checking.
Notes
- **Type first:** Define types before writing implementation code
- **Interface vs type:** Use interfaces for object types and type aliases for union types
- **Layered architecture:** Organize types, services, and components in layers
- **Hook encapsulation:** Encapsulate business logic in Hooks
Best practices:Type definitions are the foundation of a TypeScript project and should be designed carefully.
Summary
Through this comprehensive project, we have practiced the core concepts of TypeScript.
- **Type definitions:** Interfaces such as Task, CreateTaskInput, and TaskFilter
- **Service layer:** TaskService encapsulates business logic
- **Custom Hook:** useTasks manages state
- **React components:** Type-safe component development
- **Project configuration:** Strict TypeScript configuration
Note:Participate in more real projects to deepen your understanding of TypeScript through practice.