Interesting Facts About Classes and Object-Oriented Programming in TypeScript
Last Updated :
20 Mar, 2025
TypeScript extends JavaScript’s prototype-based inheritance model while also allowing developers to use class-based object-oriented programming with modern features like access modifiers, abstract classes, and interfaces.
1. Classes in TypeScript Are Syntactic Sugar
TypeScript classes simplify JavaScript’s prototype-based inheritance. When compiled, TypeScript classes are converted to functions and prototypes, making the class syntax easier to work with.
JavaScript
class MyClass {}
// Compiles to:
// var MyClass = /** @class */ (function () { ... });
2. Private Fields Use #
in JavaScript but private
in TypeScript
In TypeScript, the private keyword is used to mark class members as private. However, modern JavaScript (ES2022+) uses # to create private fields.
JavaScript
class MyClass {
private secret = "hidden";
#realSecret = "very hidden"; // JavaScript private field
}
3. Classes Can Have Readonly Properties
TypeScript allows properties to be marked as readonly, ensuring they can only be assigned once, usually in the constructor.
JavaScript
class MyClass {
readonly id: number;
constructor(id: number) {
this.id = id;
}
}
const obj = new MyClass(1);
4. Getters & Setters Control Property Access
Getters and setters in TypeScript allow validation or transformation of property values, making them more secure to modify.
JavaScript
class Person {
private _age: number = 0;
get age(): number {
return this._age;
}
set age(value: number) {
if (value < 0) {
console.log("Age can't be negative!");
} else {
this._age = value;
}
}
}
const person = new Person();
person.age = -5;
person.age = 30;
console.log(person.age);
5. Private Constructors Prevent New Instances
A class with a private constructor can’t be instantiated outside the class. This is commonly used for creating singletons.
JavaScript
class Singleton {
private static instance: Singleton;
private constructor() {}
static getInstance() {
if (!this.instance) {
this.instance = new Singleton();
}
return this.instance;
}
}
6. Classes Can Implement Multiple Interfaces
A TypeScript class can implement multiple interfaces, allowing the class to follow multiple sets of rules. However, it can only extend one class.
JavaScript
interface Flyable { fly(): void; }
interface Swimmable { swim(): void; }
class Bird implements Flyable, Swimmable {
fly() { console.log("Flying..."); }
swim() { console.log("Swimming..."); }
}
7. Classes Can Extend Built-In Types
TypeScript allows you to extend built-in JavaScript types like Array, Error, and Map, adding new features while preserving the original behavior.
JavaScript
class CustomArray extends Array {
get last() {
return this[this.length - 1];
}
}
const arr = new CustomArray(1, 2, 3);
console.log(arr.last); // 3
8. Method Overloading in TypeScript
Method overloading in TypeScript lets you define multiple function signatures. You provide a single implementation that handles all signatures.
JavaScript
class MathOps {
add(a: number, b: number): number;
add(a: string, b: string): string;
add(a: any, b: any): any {
return a + b;
}
}
const math = new MathOps();
console.log(math.add(1, 2));
console.log(math.add("Hello, ", "World!"));
9. Classes Can Be Used as Types
In TypeScript, classes can act as types, ensuring that an object is an instance of that class.
JavaScript
class Car {
drive() {
console.log("Driving...");
}
}
function startVehicle(vehicle: Car) {
vehicle.drive();
}
const myCar = new Car();
startVehicle(myCar);
10. super Calls Parent Class Methods
The super keyword in TypeScript allows child classes to call methods or constructors from their parent class.
JavaScript
class Animal {
makeSound() {
console.log("Some sound...");
}
}
class Dog extends Animal {
makeSound() {
super.makeSound();
console.log("Woof!");
}
}
Explore
TypeScript Tutorial
8 min read
TypeScript Basics
TypeScript primitive types
TypeScript Object types
TypeScript other types
TypeScript combining types
TypeScript Assertions
TypeScript Functions
TypeScript interfaces and aliases
TypeScript classes