TypeScript code is changed into JavaScript so browsers can understand it. But not everything in TypeScript becomes JavaScript code.
What survives compilation to JavaScript in 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.
interface Person is removed after compilation. Only the object user remains in JavaScript.interface Person {
name: string;
age: number;
}
const user: Person = { name: 'Alice', age: 30 };add stays in JavaScript but the type annotations are removed.function add(a: number, b: number): number { return a + b; }
type alias ID is removed. Only the variable userId remains.type ID = string | number; let userId: ID = 123;
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.
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();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.
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.
Practice
TypeScript elements does NOT survive compilation to JavaScript?Solution
Step 1: Understand TypeScript compilation
TypeScript removes type-only constructs like interfaces during compilation.Step 2: Identify which elements remain
Functions, classes, and variables are actual code and remain in JavaScript output.Final Answer:
Interfaces -> Option AQuick Check:
Interfaces are erased in JS output [OK]
- Thinking interfaces generate JavaScript code
- Confusing types with variables
- Assuming classes disappear
Solution
Step 1: Identify code vs type declarations
Types and interfaces are erased; only executable code remains.Step 2: Check each option
Only the functiongreetproduces JavaScript code.Final Answer:
function greet() { console.log('Hi'); } -> Option BQuick Check:
Functions compile to JS code [OK]
- Assuming type aliases produce JS code
- Confusing interfaces with functions
- Thinking all declarations compile to JS
interface Person { name: string; age: number; }
const user: Person = { name: 'Alice', age: 30 };
console.log(user.name);Solution
Step 1: Remove type-only declarations
Theinterface Personis removed during compilation.Step 2: Remove type annotations
The variableuserloses its: Personannotation.Final Answer:
const user = { name: 'Alice', age: 30 }; console.log(user.name); -> Option AQuick Check:
Types/interfaces removed, code remains [OK]
- Leaving interface in output
- Keeping type annotations in JS
- Expecting syntax errors for interfaces
interface Data { id: number; }
function printId(data: Data) {
console.log(data.id);
}
printId({ id: 5 });Solution
Step 1: Understand interface removal
Interfaces are erased and do not cause runtime errors.Step 2: Check function and parameter
Type annotations are removed but the function and its logic remain intact.Final Answer:
There is no error; the code compiles to valid JavaScript. -> Option DQuick Check:
Interfaces vanish, functions remain [OK]
- Thinking interfaces cause runtime errors
- Believing type annotations remain in JS
- Assuming functions get removed
type Status = 'success' | 'error';
class Response {
constructor(public status: Status) {}
print() {
console.log(`Status: ${this.status}`);
}
}
const res = new Response('success');
res.print();Solution
Step 1: Remove type alias
Thetype Statusis erased during compilation.Step 2: Remove parameter type annotation
The constructor'spublic status: Statusbecomesstatuswithout type.Step 3: Keep class and method code
The class and method remain unchanged except for type removal.Final Answer:
class Response { constructor(status) { this.status = status; } print() { console.log(`Status: ${this.status}`); } } const res = new Response('success'); res.print(); -> Option CQuick Check:
Types removed, class code remains [OK]
- Expecting type aliases in JS output
- Thinking constructor parameters keep types
- Assuming class code is removed
Ready to prove it?
Pass the test and this pattern moves unknown
