Skip to content

Compilation

TypeScript isn’t run directly β€” it’s compiled (or β€œtranspiled”) into plain JavaScript by the TypeScript compiler (tsc), which strips out all the type annotations and interfaces (they exist purely for compile-time checking) and optionally converts newer syntax down to a target JavaScript version the runtime environment supports.

input.ts
interface User {
name: string;
age: number;
}
function greet(user: User): string {
return `Hello, ${user.name}!`;
}
// output.js (after `tsc`, roughly) -- all type information is gone
function greet(user) {
return `Hello, ${user.name}!`;
}
Terminal window
tsc input.ts # compile a single file
tsc --watch # recompile automatically on save
tsc --noEmit # type-check only, don't produce JS output

Assuming TypeScript’s type checking guarantees safety at runtime β€” types are completely erased during compilation, so if untyped or mistyped data enters your program (e.g. from JSON.parse() or an API response), nothing stops it at runtime even though tsc reported no errors.

interface User { name: string; age: number; }
const response = await fetch("/api/user");
const user: User = await response.json(); // TypeScript trusts you here -- no real check!
console.log(user.age.toFixed(2)); // crashes at runtime if the API actually returned age as a string
  1. What happens to TypeScript’s type annotations when the code is compiled to JavaScript?

    AnswerThey're completely erased β€” the compiled JavaScript contains no trace of types, interfaces, or generics.
  2. What does tsc --noEmit do?

    AnswerRuns type checking without producing any JavaScript output β€” useful for CI pipelines that just want to verify the code type-checks.
  3. Why doesn’t TypeScript protect you from bad data returned by JSON.parse() or a fetch() call?

    AnswerType annotations are a compile-time-only construct; TypeScript trusts your type assertion but performs no actual runtime validation, so genuinely mismatched data still causes a runtime error.