Menu

How TypeScript Works

Lesson 2: How TypeScript Works

What happens to TypeScript code?

TypeScript code goes through two separate steps:

Step What happens Tool examples
Type checking Confirms values, functions, and objects match their types; reports errors tsc, your editor
Emitting / stripping Removes types and outputs JavaScript tsc, Node.js, esbuild, Vite, SWC

These steps are independent. Many modern tools strip types without checking them, for speed. You still run tsc (or your editor) to check.

Example

Input:

type Product = { id: string; name: string; price: number };

const milk: Product = { id: 'p1', name: 'Milk 1L', price: 64 };

function formatPrice(product: Product): string {
  return `${product.name}: ₹${product.price}`;
}

Output JavaScript:

const milk = { id: 'p1', name: 'Milk 1L', price: 64 };

function formatPrice(product) {
  return `${product.name}: ₹${product.price}`;
}

How it works

  • The type Product declaration and every : Type annotation are erased. This is called type erasure.
  • The logic is unchanged, so the output behaves like the JavaScript you would have written by hand.
  • Because types are erased, you cannot check a TypeScript type at runtime with typeof or instanceof. You check JavaScript values instead (Module 5).

Compile time versus runtime:

Compile time (TypeScript) Runtime (JavaScript)
When In the editor and during the build When the program runs
Knows about Types, interfaces, generics Values only
Errors found Type mismatches, typos, missing properties Network failures, bad user input, logic bugs

TypeScript 7: since July 2026, the official compiler is a native port written in Go. It checks code the same way as TypeScript 6.0 but is often about 10 times faster on large projects.

Common mistakes

Mistake Effect Fix
Assuming a build tool checks types Type errors reach production Run tsc --noEmit in CI
Writing if (typeof x === 'Product') Always false: types do not exist at runtime Check properties or use type guards
Ignoring errors because the JavaScript still runs Bugs ship anyway Treat type errors as build failures

Key takeaways

  • Type checking and producing JavaScript are separate steps.
  • Types are erased, so they add safety without runtime cost.

Try it: Write a small .ts file with a type and a function, compile it with npx tsc, and compare the input and output files.


TypeScript Tutorial

J

Jebasta





Get in Touch For Details