Excellent question! This gets to the very heart of how TypeScript works.
The short and direct answer is:
You compile TypeScript.
Let's break down exactly what that means and why.
The Core Concept
Think of it like this: Web browsers, Node.js, and other JavaScript environments do not understand TypeScript syntax. They only understand JavaScript.
The purpose of the "compilation" step is to translate your TypeScript code into plain JavaScript code that any browser or JavaScript runtime can execute.
So, the process looks like this:
- You Write: Code in a
.ts (TypeScript) file, using features like static types (string, number), interfaces, and enums.
- You Compile: You use the TypeScript compiler (a tool commonly called
tsc).
- The Output is: A
.js (JavaScript) file. The compiler strips out all the TypeScript-specific syntax and leaves you with clean, runnable JavaScript.
- You Run: You use this generated
.js file in your web page or run it with Node.js.
Why Not Compile JavaScript?
JavaScript is an interpreted language, not a traditionally compiled one. This means that JavaScript engines (like V8 in Chrome and Node.js) read and execute the code directly, line-by-line, without a separate, mandatory compilation step into machine code.
So, you don't "compile" JavaScript in the same way you compile TypeScript, C++, or Java. You just run it.
Nuance: In modern web development, you often use tools like Babel, Webpack, or Vite. These tools do process your JavaScript files. They might bundle them together, minify them (make them smaller), and even transpile newer JavaScript features into older versions for better browser compatibility. However, this is for optimization and compatibility, not because the browser is fundamentally unable to understand JavaScript.
It's Technically "Transpilation"
To be more precise, what the TypeScript compiler does is called transpilation, not compilation.
- Compilation: Translating a high-level language into a lower-level language (e.g., C++ to machine code).
- Transpilation: Translating a high-level language into another high-level language (e.g., TypeScript to JavaScript). It's a "source-to-source" translation.
In everyday conversation, developers often use the term "compile" for TypeScript, but "transpile" is the more accurate term.
A Simple Example
Here's what the process looks like in practice.
1. Your TypeScript Code (user.ts)
This code uses an interface and type annotations (: string, : number), which are TypeScript features.
// This is a TypeScript-only feature
interface User {
name: string;
id: number;
}
function getAdminUser(): User {
return { name: "Alice", id: 1 };
}
const user: User = getAdminUser();
// The types here prevent you from making a mistake, like user.id.toUpperCase()
console.log(user.name);
2. The Compilation Step
You would run the TypeScript compiler from your terminal:
tsc user.ts
3. The Generated JavaScript Code (user.js)
The compiler outputs this JavaScript file. Notice how the interface and all the type annotations (: User, : string, : number) have been completely removed. This is plain JavaScript that any browser can run.
// The interface is gone!
function getAdminUser() {
return { name: "Alice", id: 1 };
}
const user = getAdminUser();
// The type information is gone, but the logic remains.
console.log(user.name);
Summary Table
| Feature | TypeScript | JavaScript |
|---|
| What is it? | A superset of JavaScript that adds static types. | The standard scripting language for web development. |
| How is it executed? | Compiled/Transpiled into JavaScript before it can be run. | Interpreted directly by browsers or Node.js. |
| Key Advantage | Catches errors during development (before runtime). | Runs everywhere (universal browser compatibility). |
| Tooling | Requires the TypeScript Compiler (tsc). | Can be run directly without extra tools. |