Lesson 11 / 30

TypeScript Compilation

How Babel and SWC handle TypeScript syntax and type erasure.

Type erasure: removing types

Babel and SWC strip TypeScript syntax but do not type-check. They remove : Type, <T>, as Type, etc., leaving plain JavaScript that runs.

TypeScript to JavaScript transformation

Type annotations vanish; the logic remains. Use tsc or IDE for type checking, Babel/SWC for transpilation.

// Input: TypeScript
function greet(name: string): string {
  return `Hello, ${name}`;
}

// Output: JavaScript (types gone)
function greet(name) {
  return `Hello, ${name}`;
}

Always use tsc for type checking

Babel and SWC remove types silently. If you have a type error (wrong property name, wrong parameter type), transpilation succeeds anyway. Run tsc --noEmit in CI to catch type bugs before shipping.