The simplest way to develop a "local first" TypeScript app using just vanilla TypeScript and HTML is to use the TypeScript compiler (tsc) in "watch" mode along with a basic HTML file. This lets you write your TypeScript code, have it automatically compiled to JavaScript when you save, and then open your HTML file in a browser locally.
Below are the steps to set up such a project:
1. Install Node.js and TypeScript
-
Install Node.js
Download and install Node.js from nodejs.org. This also installs npm, the Node Package Manager.
-
Install TypeScript globally
Open your terminal (or command prompt) and run:
npm install -g typescript
This gives you access to the tsc command.
2. Set Up Your Project Files
Create a new project folder (e.g., local-ts-app) and inside it, create the following files:
a. Create app.ts (Your TypeScript Source File)
// app.ts
function displayMessage(message: string): void {
const appDiv = document.getElementById("app");
if (appDiv) {
appDiv.innerHTML = `<h1>${message}</h1>`;
}
}
displayMessage("Hello, TypeScript from a local first app!");
b. Create index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Local TypeScript App</title>
</head>
<body>
<div id="app"></div>
<!-- Include the compiled JavaScript -->
<script src="app.js"></script>
</body>
</html>
3. Compile TypeScript to JavaScript
Open your terminal in your project directory and run:
tsc app.ts
This command will compile app.ts into app.js which is referenced by your HTML file.
Tip: Use tsc in Watch Mode
For faster local development, you can use watch mode so that any changes you make to app.ts are automatically compiled:
tsc app.ts --watch
With watch mode running, simply edit your TypeScript file, save it, and refresh the browser to see the changes.
4. Run Your App Locally