Episode 2 of 21
Compiling TypeScript
Learn how to compile TypeScript files into JavaScript and set up a watch mode.
Compiling TypeScript
Browsers and Node.js don't understand TypeScript directly. You need to compile it to JavaScript first.
Basic Compilation
tsc sandbox.ts
This creates a sandbox.js file with plain JavaScript. Include that JS file in your HTML:
<script src="sandbox.js"></script>
Watch Mode
Recompiling manually every time is tedious. Use watch mode to auto-compile on save:
tsc sandbox.ts -w
Now every time you save sandbox.ts, it automatically recompiles to sandbox.js.
What Gets Compiled?
TypeScript strips all type annotations and compiles modern JS syntax down to compatible JavaScript:
// TypeScript
const greet = (name: string): string => {
return `Hello ${name}`;
};
// Compiled JavaScript
"use strict";
const greet = (name) => {
return `Hello ${name}`;
};
Key Takeaways
tsc filename.tscompiles to JavaScripttsc filename.ts -wenables watch mode- Type annotations are removed during compilation
- Modern syntax is preserved or transpiled based on config