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!");
}
}
Similar Reads
Interesting Facts About Object Types and Interfaces in TypeScript TypeScript enhances object types and interfaces with strong typing, extendibility, and dynamic features, making code more structured and maintainable. Mastering these concepts improves scalability, flexibility, and type safety in applications.1. Interfaces Can Describe FunctionsInterfaces in TypeScr
3 min read
Interesting Facts About Modules and Namespaces in TypeScript Modules and namespaces in TypeScript help organize and encapsulate code, preventing naming conflicts and improving maintainability. Modules use the ES6 import/export system, while namespaces provide internal organization within a single file. Here are some interesting facts about them:1. TypeScript
2 min read
TypeScript - Generic Functions and Generic Classes Generics in TypeScript enable functions and classes to work with multiple data types while ensuring type safety. Generic functions provide reusable logic that adapts to various inputs, while generic classes help define flexible and type-safe structures.Generic FunctionsA generic function allows you
2 min read
Interesting Facts About Generics in TypeScript TypeScript generics provide a powerful way to create reusable, type-safe components and functions that work with multiple types. They improve flexibility, reduce redundancy, and ensure strict type constraints without sacrificing reusability.1. Generics Make Functions FlexibleGenerics allow you to cr
3 min read
How to Cast a JSON Object Inside of TypeScript Class? Casting a JSON object to a TypeScript class involves converting a plain JSON object (which lacks methods and proper typing) into an instance of a class that includes all the defined methods and type safety of that class.Types of Objects in TypeScriptPlain Objects: When parsing JSON data using the JS
3 min read