give exemple of webapplication code using type script
- User
give exemple of webapplication code using type script
- Assistant
give exemple of webapplication code using type script
Creating a simple web application using TypeScript involves several components, including HTML, CSS, and TypeScript for the business logic. Below, I'll provide an example of a simple web application that allows users to add and display items in a list.
We'll create a basic to-do list application.
/todo-app
|-- index.html
|-- styles.css
|-- app.ts
|-- tsconfig.json
index.htmlThis file will contain the basic structure of our web application.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles.css">
<title>Todo List App</title>
</head>
<body>
<h1>Todo List</h1>
<input type="text" id="todo-input" placeholder="Add a new task">
<button id="add-todo">Add Todo</button>
<ul id="todo-list"></ul>
<script src="dist/app.js" defer></script>
</body>
</html>
styles.cssThis file contains some basic styles for our application.
body {
font-family: Arial, sans-serif;
margin: 20px;
}
input {
margin-right: 10px;
}
ul {
list-style-type: none;
padding: 0;
}
li {
margin: 5px 0;
}
app.tsThis is where our TypeScript code is written. It includes functionality for adding items to the list.
// app.ts
interface TodoItem {
id: number;
task: string;
}
class TodoApp {
private todoList: TodoItem[] = [];
private todoInput: HTMLInputElement;
private todoListElement: HTMLUListElement;
private addTodoButton: HTMLButtonElement;
constructor() {
this.todoInput = document.getElementById('todo-input') as HTMLInputElement;
this.todoListElement = document.getElementById('todo-list') as HTMLUListElement;
this.addTodoButton = document.getElementById('add-todo') as HTMLButtonElement;
this.addTodoButton.addEventListener('click', () => this.addTodo());
}
private addTodo() {
const task = this.todoInput.value.trim();
if (task) {
const newTodo: TodoItem = {
id: Date.now(),
task: task,
};
this.todoList.push(newTodo);
this.renderTodoList();
this.todoInput.value = '';
}
}
private renderTodoList() {
this.todoListElement.innerHTML = '';
this.todoList.forEach((todo) => {
const listItem = document.createElement('li');
listItem.textContent = todo.task;
this.todoListElement.appendChild(listItem);
});
}
}
const app = new TodoApp();
tsconfig.jsonThis file is used to configure the TypeScript compiler.
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"outDir": "./dist",
"rootDir": "./",
"strict": true,
"esModuleInterop": true
},
"include": [
"*.ts"
],
"exclude": [
"node_modules"
]
}
Install TypeScript globally if you haven’t already:
npm install -g typescript
Navigate to your project directory and initialize a new Node.js project:
npm init -y
Compile the TypeScript file:
tsc
Open the index.html file in your web browser.
You now have a simple To-Do List application built with TypeScript. It allows users to input tasks, which are then displayed in a list format. You can expand this by adding features like deleting tasks, marking them as complete, or storing them in local storage.
Not yet included in a published release.