0
0
Typescriptprogramming~5 mins

What survives compilation to JavaScript in Typescript

Choose your learning style9 modes available
Introduction

TypeScript code is changed into JavaScript so browsers can understand it. But not everything in TypeScript becomes JavaScript code.

When you want to know which parts of your TypeScript code will run in the browser.
When debugging JavaScript generated from TypeScript.
When learning how TypeScript helps without adding extra code.
When optimizing your code size by understanding what stays after compiling.
Syntax
Typescript
// TypeScript code example
let x: number = 5;
function greet(name: string): void {
  console.log(`Hello, ${name}!`);
}

// After compilation, types and interfaces are removed.

TypeScript types and interfaces do not appear in the JavaScript output.

Functions and variables remain as JavaScript code.

Examples
The interface Person is removed after compilation. Only the object user remains in JavaScript.
Typescript
interface Person {
  name: string;
  age: number;
}

const user: Person = { name: 'Alice', age: 30 };
The function add stays in JavaScript but the type annotations are removed.
Typescript
function add(a: number, b: number): number {
  return a + b;
}
The type alias ID is removed. Only the variable userId remains.
Typescript
type ID = string | number;

let userId: ID = 123;
Sample Program

This TypeScript code defines an interface and a class. After compilation, the interface is removed but the class and its methods remain as JavaScript code.

Typescript
interface Animal {
  name: string;
  speak(): void;
}

class Dog implements Animal {
  constructor(public name: string) {}
  speak() {
    console.log(`${this.name} says Woof!`);
  }
}

const dog = new Dog('Buddy');
dog.speak();
OutputSuccess
Important Notes

TypeScript's types, interfaces, and enums are removed during compilation.

Classes, functions, variables, and actual code logic remain in JavaScript.

TypeScript helps catch errors before running code but does not add extra code to JavaScript output.

Summary

TypeScript types and interfaces do not survive compilation.

Actual code like functions, classes, and variables remain in JavaScript.

Compilation removes only type information, keeping runnable code intact.