← Back to libraryQuestion 227 of 468
🔷TypeScriptBeginner

How TypeScript Compiles to JavaScript

📌 Definition:

TypeScript code is compiled (transpiled) to plain JavaScript by the tsc compiler (or tools like esbuild/swc/babel). Types are ERASED — the output is standard JS that runs anywhere JS runs.

📖 Detailed Explanation:

tsc reads tsconfig.json, type-checks the project, and emits .js files (and optionally .d.ts declaration files and source maps) targeting the configured ES version. Type annotations, interfaces, and type-only imports vanish in the output — they exist purely for compile-time checking and tooling. This 'type erasure' is why there's no runtime type enforcement. For tests you can run TS directly via ts-node or a framework's built-in TS support (Playwright/Cypress bundle it) without a manual compile step. Declaration files (.d.ts) let JS libraries ship types. Because output is plain JS, TypeScript adds zero runtime dependency.

🔑 Key Points:
  • tsc type-checks then emits plain JavaScript (types erased)
  • No runtime type enforcement — types are compile-time only
  • ts-node / framework tooling run TS without a manual build
  • .d.ts files provide types for JS libraries
🌍 Real-World Example:

A Playwright TS test runs directly (npx playwright test) because Playwright transpiles TS on the fly — the types guide the editor and compiler, and the executed code is the erased JavaScript.

🎯 Scenario-Based Interview Question:

Does a TypeScript interface exist in the compiled JavaScript at runtime? Why does the answer matter for validation?