<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Rawan Aboegila</title>
    <description>The latest articles on Forem by Rawan Aboegila (@raboegila).</description>
    <link>https://forem.com/raboegila</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1056478%2Fba6496fd-639e-41c8-bb06-e7b10ebc8040.png</url>
      <title>Forem: Rawan Aboegila</title>
      <link>https://forem.com/raboegila</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/raboegila"/>
    <language>en</language>
    <item>
      <title>The OOP Fight : JavaScript Vs TypeScript</title>
      <dc:creator>Rawan Aboegila</dc:creator>
      <pubDate>Sat, 01 Apr 2023 12:53:21 +0000</pubDate>
      <link>https://forem.com/raboegila/oop-typescript-vs-javascript-4p78</link>
      <guid>https://forem.com/raboegila/oop-typescript-vs-javascript-4p78</guid>
      <description>&lt;p&gt;When I first started learning about JavaScript, I couldn’t help but notice how different it interacts with objects and entities compared to other languages that support object-oriented programming like C++ and Java, and how JavaScript is always categorized as an OOP Language by many. However, it has a different nature, as it is a special type of OOP that is &lt;strong&gt;prototype-based&lt;/strong&gt; not the &lt;strong&gt;&lt;em&gt;classical&lt;/em&gt; class-based&lt;/strong&gt; OOP that we are mostly familiar with. That means that JS supports both functional and object-oriented programming; and that's why it handles objects and entities in a slightly different way.&lt;/p&gt;

&lt;p&gt;However, despite the fact that TypeScript is a superset of JavaScript,  it offers several advantages over its predecessor, particularly when it comes to object-oriented programming. In this article, we will explore how TypeScript has enhanced OOP capabilities in comparison to JavaScript, and how it conforms to the SOLID principles with code examples.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Table Of Contents
&lt;/h2&gt;



&lt;ul&gt;
&lt;li&gt;
TypeScript Key Features

&lt;ul&gt;
&lt;li&gt;Strongly Typed Language&lt;/li&gt;
&lt;li&gt;Classes and Interfaces&lt;/li&gt;
&lt;li&gt;
Classes

&lt;ul&gt;
&lt;li&gt;Constructor Function in JavaScript&lt;/li&gt;
&lt;li&gt;Classes in JavaScript&lt;/li&gt;
&lt;li&gt;Classes in TypeScript&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
Interfaces 

&lt;ul&gt;
&lt;li&gt;Type Aliases&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Access Modifiers &lt;/li&gt;
&lt;li&gt;Inheritance and Polymorphism&lt;/li&gt;
&lt;li&gt;Abstract Classes&lt;/li&gt;
&lt;li&gt;Generics&lt;/li&gt;
&lt;li&gt;Intersection and Union Types&lt;/li&gt;
&lt;li&gt;Decorators&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
SOLID Principles 

&lt;ul&gt;
&lt;li&gt;Single Responsibility Principle (SRP)&lt;/li&gt;
&lt;li&gt;Open-Closed Principle (OCP)&lt;/li&gt;
&lt;li&gt;Liskov Substitution Principle (LSP)&lt;/li&gt;
&lt;li&gt;Interface Segregation Principle (ISP)&lt;/li&gt;
&lt;li&gt;Dependency Inversion Principle (DIP)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;So .. Who Wins?&lt;/li&gt;
&lt;li&gt;References&lt;/li&gt;
&lt;/ul&gt;







&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  TypeScript Key Features
&lt;/h2&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  First : Strongly Typed Language
&lt;/h3&gt;

&lt;p&gt;One of the most significant advantages of TypeScript over JavaScript is that it is a strongly typed language. This means that TS enforces types on variables, functions, and classes, ensuring that the code is more robust and less error-prone. In contrast, JavaScript is a dynamically typed language, which means that variables can change their types at runtime, leading to more bugs and less maintainable code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// JavaScript
function add(a, b) {
  return a + b;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// TypeScript
function add(a: number, b: number): number {
  return a + b;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;blockquote&gt;
&lt;p&gt;In the above example, the TypeScript function add has types for its parameters (a and b) and return value. This makes it easier to understand the function's behavior and catch errors at compile-time. &lt;/p&gt;
&lt;/blockquote&gt;








&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Second : Classes and Interfaces
&lt;/h3&gt;

&lt;p&gt;Classes allow you to define blueprints for objects with properties and methods, while interfaces define the structure and types of objects. These features are mostly not available in JavaScript, which uses prototype-based inheritance instead of class-based inheritance.&lt;br&gt;
&lt;br&gt;&lt;br&gt;
&lt;a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Classes
&lt;/h4&gt;

&lt;p&gt;According to &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes"&gt;MDN&lt;/a&gt;, the JS class implementation is introduced in 2015, in ES6. It is based on the notation that classes are just &lt;em&gt;special functions&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Classes are in fact "special functions", and just as you can define function expressions and function declarations, a class can be defined in two ways: a class expression or a class declaration.&lt;/p&gt;
&lt;/blockquote&gt;



&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h5&gt;
  
  
  Constructor Function in JavaScript
&lt;/h5&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// JavaScript
function Animal(name) {
  this.name = name;
}

Animal.prototype.speak = function() {
  console.log(`${this.name} makes a noise.`);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Since TypeScript is a subset of JavaScript, under the hood, it implements classes the same way JS does. However, due to TS strongly typed nature, there are several properties added on top of JS that enhances readability, maintainability and reduces errors. In other words, TypeScript has full support for that syntax and also adds features on top of it, like member visibility, abstract classes, generic classes, arrow function methods, and a few others.&lt;br&gt;
&lt;br&gt;&lt;br&gt;
&lt;a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h5&gt;
  
  
  Classes in JavaScript
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// JavaScript

class Rectangle {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  Classes in TypeScript
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Animal {
  name: string;

  constructor(name: string) {
    this.name = name;
  }

  speak(): void {
    console.log(`${this.name} makes a noise.`);
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, there isn't a big difference in the general syntax between JS and TS when it comes to classes. However TypeScript Classes can be considered more readable, maintainable and less error prune. As we go through the key features of TypeScript, we will explore how TypeScript has full support for the class-based OOP.&lt;br&gt;
&lt;br&gt;&lt;br&gt;
&lt;a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Interfaces
&lt;/h4&gt;

&lt;p&gt;JavaScript does not have support for interfaces, as it's inheritance is based on objects and not classes and due to it's dynamic nature. Moreover, JavaScript uses &lt;strong&gt;&lt;a href="https://en.wikipedia.org/wiki/Duck_typing"&gt;duck typing&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If it walks like a duck, and quacks like a duck, as far as JS cares, it’s a duck.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;On the other hand, TypeScript has support for interfaces. &lt;code&gt;interface&lt;/code&gt; and &lt;code&gt;type&lt;/code&gt; are how TS can enforce structure on objects, making sure that objects have the expected properties.&lt;/p&gt;

&lt;p&gt;TypeScript interfaces supports interface extending and adding fields to an existing interface&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// JavaScript
function printLabel(labelObj) {
  console.log(labelObj.label);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// TypeScript
interface LabelObj {
  label: string;
}

function printLabel(labelObj: LabelObj): void {
  console.log(labelObj.label);
}

printLabel({ label:"name" })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;In the above example, the JavaScript &lt;code&gt;printLabel&lt;/code&gt; function takes an object with a &lt;code&gt;label&lt;/code&gt; property. In TypeScript, an &lt;code&gt;interface&lt;/code&gt; is defined for this object to ensure that it has a &lt;code&gt;label&lt;/code&gt; property of type string. This makes it easier to catch errors at compile-time and write more maintainable code.  &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  Type Aliases
&lt;/h5&gt;

&lt;p&gt;Types are another example of how Typescript enforces object structure. &lt;/p&gt;

&lt;p&gt;According to &lt;a href="https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#interfaces"&gt;TypeScript Documentation &lt;/a&gt; :&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Almost all features of an interface are available in type, the key distinction is that a type cannot be re-opened to add new properties vs an interface which is always extendable&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here's an example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface Animal {
  name: string
}

interface Bear extends Animal {
  honey: boolean
}

interface Animal {
favFood: string

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Animal = {
  name: string
}

type Bear = Animal &amp;amp; { 
  honey: boolean 
}

interface Animal {
favFood: string

}
 // Error: Duplicate identifier 'Animal'.

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;








&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Third : Access Modifiers
&lt;/h3&gt;

&lt;p&gt;TypeScript also offers access modifiers, which allow you to restrict access to certain properties and methods of a class. Access modifiers like private and protected help to enforce encapsulation and prevent unauthorized access to the internal state of an object. JavaScript does not have access modifiers, making it more difficult to achieve encapsulation and maintain a clean code base.&lt;br&gt;
&lt;br&gt;&lt;br&gt;
Here is an example of how TypeScript access modifiers can help with encapsulation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// JavaScript

function Person(age) {
   This.age =age;
}


Person.prototype.getAge = function() {
   Return this.age
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;br&gt;&lt;br&gt;
Let's say that you want &lt;code&gt;Person&lt;/code&gt; to be a class with a a private property called &lt;code&gt;age&lt;/code&gt;. You want to create a method called &lt;code&gt;getAge()&lt;/code&gt; that returns the value of the &lt;code&gt;age&lt;/code&gt; property, but you don't want to allow direct access to the &lt;code&gt;age&lt;/code&gt; property from outside the class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// TypeScript
class Person {
 private age: number;


 constructor(age: number) {
   this.age = age;
 }


 public get getAge(): number {
   return this.age;
 }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By using access modifiers like &lt;code&gt;private&lt;/code&gt;, &lt;code&gt;public&lt;/code&gt;, and &lt;code&gt;protected&lt;/code&gt;, you can control access to the internal state of an object, ensuring that it is only modified in a controlled manner. This helps to maintain encapsulation, which is an essential principle of OOP that promotes modularity, maintainability, and robustness.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;

&lt;p&gt;Moreover, TypeScript has two additional access modifiers than the popular three mentioned above: &lt;code&gt;static&lt;/code&gt; and &lt;code&gt;readonly&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;static&lt;/code&gt; properties: they can only be accessed from the class, not from an object instantiated from it and they can not be inherited.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;readonly&lt;/code&gt; properties: these properties can not be modified after initialization, they can only be read. They must be set at the class declaration or inside the constructor &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The example shows how the TS uses access modifiers inside the class. Note that &lt;code&gt;this&lt;/code&gt; keyword can not be used to reference static properties and methods.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//TypeScript

class Employee {
private name: string;
readonly ssn : string;
private static  count: number =  0;


constructor(name:string, ssn:string){
this.name = name
this.ssn = ssn
Employee.count =+1;
}
public getName():string {
return this.name;
}
public static getCount():number {
   return Employee.count;
}
public getSSN():string{
   return this.ssn;
}


};


let emp:Employee = new Employee("Mike", "836527496");


//emp.count; // 'count' does not exist on type 'Employee'
//emp.ssn=5; // Cannot assign to 'ssn' because it is a read-only property

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;br&gt;&lt;br&gt;
Now as mentioned before, JS &lt;code&gt;class&lt;/code&gt; support was introduced in ES6, and with it some features of classes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;static&lt;/code&gt; keyword&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;private access modifier using &lt;code&gt;#&lt;/code&gt; notation&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;get&lt;/code&gt; and &lt;code&gt;set&lt;/code&gt; keywords&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here's an example demonstrating the added features:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//JavaScript

class Time {
    #hour = 0
    #minute = 0
    #second = 0
    static #GMT = 0
    constructor(hour, minute, second) {
        this.#hour = hour
        this.#minute = minute
        this.#second = second
    }
    static  get displayGMT() {
        return Time.#GMT
    }

    static set setGMT(gmt){
      Time.#GMT=gmt
    }
    get getHour(){
      return this.#hour
    }
   set setHour(hour){
       this.#hour=hour
    }

}
//console.log(Time.getHour()) // Error: Time.getHour is not a function
console.log(Time.getHour) //undefined
console.log(Time.displayGMT) //0
Time.setGMT = 2
myTime = new Time()
console.log(Time.#hour) //Error: reference to undeclared private field or method #hour
console.log(myTime.#hour) //Error: reference to undeclared private field or method #hour
console.log(Time.displayGMT) //2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Despite having these features, their behavior is quite confusing to grasp at the first glance, moreover it's readability is less than it's TypeScript equivalent.&lt;/p&gt;
&lt;/blockquote&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//TypeScript

class Time {
    private hour: number = 0
    private minute: number = 0
    private second: number = 0
    private static GMT: number = 0
    constructor(hour: number, minute: number, second: number) {
        this.hour = hour
        this.minute = minute
        this.second = second
    }
    static get displayGMT(): number {
        return Time.GMT
    }

    static set setGMT(gmt: number) {
        Time.GMT = gmt
    }
    get getHour(): number {
        return this.hour
    }
    set setHour(hour: number) {
        this.hour = hour
    }

}

//console.log(Time.getHour()) //Property 'getHour' does not exist on type 'typeof Time'.
console.log(Time.displayGMT) //0
Time.setGMT = 2
//myTime = new Time() //Expected 3 arguments, but got 0.
const myTime: Time = new Time(1, 2, 3)
//console.log(Time.hour) //Property 'hour' does not exist on type 'typeof Time'
//console.log(myTime.hour) //Property 'hour' is private and only accessible within class 'Time'.
console.log(Time.displayGMT) //2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;








&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Fourth : Inheritance and Polymorphism
&lt;/h3&gt;

&lt;p&gt;TypeScript supports inheritance and polymorphism, two essential concepts in OOP. Inheritance allows you to create a hierarchy of classes, with each subclass inheriting properties and methods from its parent class.&lt;/p&gt;

&lt;p&gt;Polymorphism allows you to use a subclass object in place of a parent class object, making the code more flexible and reusable. JavaScript does support inheritance and polymorphism, but it uses a less intuitive and more verbose syntax.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// JavaScript
function Animal(name) {
 this.name = name;
}


Animal.prototype.speak = function() {
 console.log(`${this.name} makes a noise.`);
}


function Dog(name) {
 Animal.call(this, name);
}


Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;


Dog.prototype.speak = function() {
 console.log(`${this.name} barks.`);
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;In the JavaScript example, inheritance is implemented using prototype-based inheritance, which can be difficult to read and maintain.  &lt;/p&gt;
&lt;/blockquote&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// TypeScript
class Animal {
 name: string;


 constructor(name: string) {
   this.name = name;
 }


 speak(): void {
   console.log(`${this.name} makes a noise.`);
 }
}


class Dog extends Animal {
 constructor(name: string) {
   super(name);
 }


 speak(): void {
   console.log(`${this.name} barks.`);
 }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;In TypeScript, inheritance is implemented using the extends keyword, which makes the code more readable and easier to maintain. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
With the introduction of classes in ES6, &lt;code&gt;extends&lt;/code&gt; keyword became supported as well with &lt;code&gt;class&lt;/code&gt; implementations.&lt;/p&gt;

&lt;p&gt;Here's an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Cat {
  constructor(name) {
    this.name = name;
  }

  speak() {
    console.log(`${this.name} makes a noise.`);
  }
}

class Lion extends Cat {
  speak() {
    super.speak();
    console.log(`${this.name} roars.`);
  }
}

const myLion = new Lion("Fuzzy");
// Fuzzy makes a noise.

myLion.speak();  // Fuzzy roars.


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;








&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Fifth : Abstract Classes
&lt;/h3&gt;

&lt;p&gt;TypeScript supports abstract classes, which are classes that cannot be instantiated directly. Abstract classes can only be used as base classes for other classes. This allows developers to define common functionality for a group of related classes without having to implement the functionality in each class. &lt;/p&gt;

&lt;p&gt;Here's an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// JavaScript
 function Vehicle()  
{  
    this.vehicleName="vehicleName";  
    throw new Error("You cannot create an instance of Abstract Class");  
}  
Vehicle.prototype.display=function()  
{  
    return "Vehicle is: "+this.vehicleName;  
}  
//Creating a constructor function  
function Bike(vehicleName)  
{  
    this.vehicleName=vehicleName;  
}  
//Creating object without using the function constructor  
Bike.prototype=Object.create(Vehicle.prototype); 

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;There is no direct JS Abstract class implementation, however there are ways/ workarounds that can be implemented to apply abstraction. Here in the JavaScript example above, we created a constructor function that throws an error if the constructor is called. And when it comes to the subclass &lt;code&gt;Bike&lt;/code&gt;, using it’s prototype and accessing the Object create function to set it to the base class prototype.&lt;/p&gt;
&lt;/blockquote&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// TypeScript
abstract class Animal {
  name: string;

  constructor(name: string) {
    this.name = name;
  }

  abstract speak(): void;
}

class Dog extends Animal {
  constructor(name: string) {
    super(name);
  }

  speak(): void {
    console.log(`${this.name} barks.`);
  }
}

class Cat extends Animal {
  constructor(name: string) {
    super(name);
  }

  speak(): void {
    console.log(`${this.name} meows.`);
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Unlike JavaScript, Typescript offers easy, direct and readable class abstraction implementation.  &lt;/p&gt;
&lt;/blockquote&gt;






&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Sixth : Generics
&lt;/h3&gt;

&lt;p&gt;TypeScript also supports generics, which allow you to create reusable code that can work with different types of data. Generics are especially useful when working with collections like arrays, where you may want to define a function or class that can work with any type of data. JavaScript does not have generics, making it more difficult to write generic code.&lt;/p&gt;

&lt;p&gt;Here's a simple example of generics in TS:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// TypeScript
function identity&amp;lt;T&amp;gt;(arg: T): T {
 return arg;
}


let output1 = identity&amp;lt;string&amp;gt;("Hello");
let output2 = identity&amp;lt;number&amp;gt;(42);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;br&gt;&lt;br&gt;
However, this example can easily be created in JS with similar results, thus doesn’t really show why Generics are important, why their absence from JavaScript makes it difficult to write reusable strongly typed code.&lt;/p&gt;

&lt;p&gt;Here's how :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// JavaScript
function identity(arg) {
   return arg;
}
let output1 = identity("Hello");
let output2 = identity(42);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Now let's see their power in the example below:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//JavaScript

function getArray(arr) {
   return new Array().concat(arr);
}
let myNumArr = getArray([10, 20, 30]);
let myStrArr = getArray(["Hello", "JavaScript"]);
myNumArr.push(40); // Correct 
myNumArr.push("Hello OOP"); // Correct 
myStrArr.push("Hello TypeScript"); // Correct 
myStrArr.push(40); // Correct 
console.log(myNumArr); // [10, 20, 30, 40, "Hello OOP"] 
console.log(myStrArr); // ["Hello", "JavaScript", "Hello TypeScript", 40]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;br&gt;&lt;br&gt;
In the above example, the &lt;code&gt;getArray()&lt;/code&gt; function accepts an array. The &lt;code&gt;getArray()&lt;/code&gt; function creates a new array, concatenates items to it and returns this new array. Since JavaScript doesn’t define data type enforcement, we can pass any type of items to the function. But, this may not be the correct way to add items. We have to add numbers to &lt;code&gt;myNumArr&lt;/code&gt; and the strings to &lt;code&gt;myStrArr&lt;/code&gt;, but we do not want to add numbers to the &lt;code&gt;myStrArr&lt;/code&gt; or vice-versa.&lt;/p&gt;

&lt;p&gt;To solve this, TypeScript introduced generics. With generics, the type variable only accepts the particular type that the user provides at declaration.&lt;/p&gt;

&lt;p&gt;Here's the TypeScript to the previous example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//TypeScript

function getArray&amp;lt;T&amp;gt;(arr: T[]): T[] {
    return new Array&amp;lt;T&amp;gt;().concat(arr);
}
let myNumArr = getArray&amp;lt;number&amp;gt;([10, 20, 30]);
let myStrArr = getArray&amp;lt;string&amp;gt;(["Hello", "JavaScript"]);
myNumArr.push(40); // Correct  
myNumArr.push("Hi! OOP"); // Error: Argument of type 'string' is not assignable to parameter of type 'number'  
myStrArr.push("Hello TypeScript"); // Correct  
myStrArr.push(50); // Error: Argument of type 'number' is not assignable to parameter of type 'string'

console.log(myNumArr);
console.log(myStrArr);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;








&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Seventh : Intersection and Union Types
&lt;/h3&gt;

&lt;p&gt;TypeScript supports intersection and union types, which are a way to combine types to create new types. Intersection types allow developers to create types that have all the properties and methods of two or more types. Union types allow developers to create types that can be one of two or more types.&lt;/p&gt;

&lt;p&gt;Here's an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// TypeScript
interface Dog {
  name: string;
  breed: string;
}

interface Cat {
  name: string;
  color: string;
}

type Pet = Dog &amp;amp; Cat; // Intersection
type DogOrCat = Dog | Cat; // Union

let myPet: Pet = { name: "Fluffy", breed: "Poodle", color: "White" }; 
// Error: Property 'color' is missing in type 'Pet' but required in type 'Cat'.

let myDogOrCat: DogOrCat = { name: "Mittens", breed: "Tabby" };

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;In the above example, the &lt;code&gt;Dog&lt;/code&gt; and &lt;code&gt;Cat&lt;/code&gt; interfaces are defined with different properties. The &lt;code&gt;Pet&lt;/code&gt; type is defined as an intersection of the &lt;code&gt;Dog&lt;/code&gt; and &lt;code&gt;Cat&lt;/code&gt; interfaces, which means it has all the properties of both interfaces. The &lt;code&gt;DogOrCat&lt;/code&gt; type is defined as a union of the &lt;code&gt;Dog&lt;/code&gt; and &lt;code&gt;Cat&lt;/code&gt; interfaces, which means it can be either a dog or a cat. The &lt;code&gt;myPet&lt;/code&gt; variable is assigned an object that has all the properties of both the &lt;code&gt;Dog&lt;/code&gt; and &lt;code&gt;Cat&lt;/code&gt; interfaces, but TypeScript throws a compiler error because the color property is missing. The &lt;code&gt;myDogOrCat&lt;/code&gt; variable is assigned an object that has the properties of the &lt;code&gt;Dog&lt;/code&gt; interface.    &lt;/p&gt;
&lt;/blockquote&gt;



&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt; : It's not possible to define intersection and union types in JavaScript&lt;br&gt;
&lt;br&gt;&lt;/p&gt;



&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Eighth : Decorators
&lt;/h3&gt;

&lt;p&gt;TypeScript supports decorators, which are a way to add metadata to classes, methods, and properties. Decorators allow developers to write code that is more declarative and flexible. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Several libraries and frameworks are built based on this powerful feature, for example: &lt;em&gt;Angular&lt;/em&gt; and &lt;em&gt;Nestjs&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Here is a simple method decorator example to demonstrate the syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function myDecorator(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
 console.log(`Decorating ${target.constructor.name}.${propertyKey}`);
}
class MyClass {
 @myDecorator
 myMethod() {
   console.log("Hello, world!");
 }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;In the above example, the &lt;code&gt;myDecorator&lt;/code&gt; function is defined with three parameters: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;target&lt;/code&gt; (the class constructor)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;propertyKey&lt;/code&gt; (the name of the decorated method)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;descriptor&lt;/code&gt; (an object that describes the decorated method). &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;myMethod&lt;/code&gt; method of the &lt;code&gt;MyClass&lt;/code&gt; class is decorated with the &lt;code&gt;myDecorator&lt;/code&gt; function. When the &lt;code&gt;myMethod&lt;/code&gt; method is called, the decorator logs a message to the console.  &lt;/p&gt;
&lt;/blockquote&gt;



&lt;p&gt;Now let’s review a more advanced example that shows an actual decorator use case, this example uses parameter decorator and method decorator&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import "reflect-metadata";
const requiredMetadataKey = Symbol("required");
function required(target: Object, propertyKey: string | symbol, parameterIndex: number) {
 let existingRequiredParameters: number[] = Reflect.getOwnMetadata(requiredMetadataKey, target, propertyKey) || [];
 existingRequiredParameters.push(parameterIndex);
 Reflect.defineMetadata( requiredMetadataKey, existingRequiredParameters, target, propertyKey);
}
function validate(target: any, propertyName: string, descriptor: TypedPropertyDescriptor&amp;lt;Function&amp;gt;) {
 let method = descriptor.value!;
 descriptor.value = function () {
   let requiredParameters: number[] = Reflect.getOwnMetadata(requiredMetadataKey, target, propertyName);
   console.log("paramas", requiredParameters)
   if (requiredParameters) {
     for (let parameterIndex of requiredParameters) {
       if (parameterIndex &amp;gt;= arguments.length || arguments[parameterIndex] === undefined) {
         throw new Error("Missing required argument.");
       }
     }
   }
   return method.apply(this, arguments);
 };
}


class BugReport {
 type = "report";
 title: "string;"
 constructor(t: string) {
   this.title = t;
 }


 @validate
 print(@required verbose: boolean) {
   if (verbose) {
     return `type: ${this.type}\ntitle: "${this.title}`;"
   } else {
    return this.title;
   }
 }
}


const br = new BugReport("Bug Report Message")


console.log(br.print()) // [ERR]: Missing required argument.
console.log(br.print(true)) // [LOG]: "type: report title: "Bug Report Message\""
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;The &lt;code&gt;@required&lt;/code&gt; decorator adds a metadata entry that marks the parameter as required. The &lt;code&gt;@validate&lt;/code&gt; decorator then wraps the existing &lt;code&gt;greet&lt;/code&gt; method in a function that validates the arguments before invoking the original method.&lt;/p&gt;
&lt;/blockquote&gt;



&lt;p&gt;It’s important to mention that decorators, while useful and revolutionary, are an experimental feature in TypeScript. To use them you need to set the compiler target in &lt;code&gt;tsconfig.ts&lt;/code&gt; to ES5 or higher and enable the &lt;code&gt;experimentalDecorators&lt;/code&gt;, moreover, in the previous example you will also need to install the &lt;code&gt;reflect-metadata&lt;/code&gt; library and enable the &lt;code&gt;emitDecoratorMetadata&lt;/code&gt; in the &lt;code&gt;tsconfig.ts&lt;/code&gt; file as well&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;&lt;br&gt;
I highly suggest that you check out the full documentation and article on decorators for more detailed explanations and examples. To do so, follow these links:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://mirone.me/a-complete-guide-to-typescript-decorator/"&gt;https://mirone.me/a-complete-guide-to-typescript-decorator/&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.typescriptlang.org/docs/handbook/decorators.html"&gt;https://www.typescriptlang.org/docs/handbook/decorators.html&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;



&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  SOLID Principles
&lt;/h2&gt;

&lt;p&gt;SOLID is an acronym for five Object-Oriented design principles that aim to make software systems more maintainable, extensible, and robust. These principles are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;S - Single Responsibility Principle (SRP)&lt;/li&gt;
&lt;li&gt;O - Open-Closed Principle (OCP)&lt;/li&gt;
&lt;li&gt;L - Liskov Substitution Principle (LSP)&lt;/li&gt;
&lt;li&gt;I - Interface Segregation Principle (ISP)&lt;/li&gt;
&lt;li&gt;D - Dependency Inversion Principle (DIP)&lt;/li&gt;
&lt;/ul&gt;



&lt;p&gt;Let's see how TypeScript conforms to these principles with code examples.&lt;/p&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  First : Single Responsibility Principle (SRP)
&lt;/h3&gt;

&lt;p&gt;The SRP states that a class should have only one reason to change. In other words, a class should be responsible for only one thing. This principle helps to promote modularity, maintainability, and testability.&lt;br&gt;
In TypeScript, you can use classes and modules to achieve the SRP.&lt;/p&gt;

&lt;p&gt;Here's an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Account.ts
export class Account {
  private balance: number;

  constructor(balance: number) {
    this.balance = balance;
  }

  public getBalance(): number {
    return this.balance;
  }

  public deposit(amount: number): void {
    this.balance += amount;
  }

  public withdraw(amount: number): void {
    if (amount &amp;gt; this.balance) {
      throw new Error('Insufficient funds');
    }

    this.balance -= amount;
  }
}

// Bank.ts
import { Account } from './Account';

export class Bank {
  private accounts: Account[];

  constructor() {
    this.accounts = [];
  }

  public openAccount(balance: number): Account {
    const account = new Account(balance);
    this.accounts.push(account);
    return account;
  }

  public closeAccount(account: Account): void {
    const index = this.accounts.indexOf(account);
    if (index === -1) {
      throw new Error('Account not found');
    }

    this.accounts.splice(index, 1);
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;In this example, the &lt;code&gt;Account&lt;/code&gt; class is responsible for managing an account's balance, while the &lt;code&gt;Bank&lt;/code&gt; class is responsible for managing a collection of accounts. By separating these responsibilities into different classes, we ensure that each class has only one reason to change, making the code more modular, maintainable, and testable. &lt;/p&gt;
&lt;/blockquote&gt;






&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Second : Open-Closed Principle (OCP)
&lt;/h3&gt;

&lt;p&gt;The OCP states that a class should be open for extension but closed for modification. In other words, you should be able to add new functionality to a class without modifying its existing code. This principle helps to promote extensibility and maintainability.&lt;br&gt;
In TypeScript, you can use inheritance and interfaces to achieve the OCP.&lt;/p&gt;

&lt;p&gt;Here's an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Shape.ts
export interface Shape {
  getArea(): number;
}

// Rectangle.ts
import { Shape } from './Shape';

export class Rectangle implements Shape {
  private width: number;
  private height: number;

  constructor(width: number, height: number) {
    this.width = width;
    this.height = height;
  }

  public getArea(): number {
    return this.width * this.height;
  }
}

// Circle.ts
import { Shape } from './Shape';

export class Circle implements Shape {
  private radius: number;

  constructor(radius: number) {
    this.radius = radius;
  }

  public getArea(): number {
    return Math.PI * this.radius ** 2;
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;In this example, the &lt;code&gt;Shape&lt;/code&gt; interface defines a contract for shapes that have an area. The &lt;code&gt;Rectangle&lt;/code&gt; and &lt;code&gt;Circle&lt;/code&gt; classes implement the &lt;code&gt;Shape&lt;/code&gt; interface, providing their own implementations of the &lt;code&gt;getArea()&lt;/code&gt; method. By using the &lt;code&gt;Shape&lt;/code&gt; interface, we can add new shapes to the system without modifying the existing code, making the code more extensible and maintainable.&lt;/p&gt;
&lt;/blockquote&gt;






&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Third : Liskov Substitution Principle (LSP)
&lt;/h3&gt;

&lt;p&gt;The LSP states that a subclass should be substitutable for its parent class without affecting the correctness of the program. In other words, you should be able to use a subclass object in place of a parent class object without causing any problems. This principle helps to promote polymorphism and extensibility.&lt;br&gt;
In TypeScript, you can use inheritance and interfaces to achieve the LSP&lt;/p&gt;

&lt;p&gt;Here's an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Vehicle.ts
export class Vehicle {
  public startEngine(): void {
    console.log('Engine started');
  }

  public stopEngine(): void {
    console.log('Engine stopped');
  }
}

// Car.ts
import { Vehicle } from './Vehicle';

export class Car extends Vehicle {
  public drive(): void {
    console.log('Driving');
  }
}

// RaceCar.ts
import { Car } from './Car';

export class RaceCar extends Car {
  public drive(): void {
    console.log('Driving fast

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;In this example, the &lt;code&gt;Vehicle&lt;/code&gt; class defines a basic set of methods for a vehicle, including starting and stopping the engine. The &lt;code&gt;Car&lt;/code&gt; class extends the &lt;code&gt;Vehicle&lt;/code&gt; class, adding a &lt;code&gt;drive()&lt;/code&gt; method that is specific to cars. The &lt;code&gt;RaceCar&lt;/code&gt; class extends the Car class, adding a &lt;code&gt;drive()&lt;/code&gt; method that is specific to race cars.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;By using inheritance and the LSP, we can use a &lt;code&gt;RaceCar&lt;/code&gt; object in place of a &lt;code&gt;Vehicle&lt;/code&gt; or &lt;code&gt;Car&lt;/code&gt; object without affecting the correctness of the program. This promotes polymorphism and extensibility, making the code more flexible and reusable.  &lt;/p&gt;






&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Fourth : Interface Segregation Principle (ISP)
&lt;/h3&gt;

&lt;p&gt;The ISP states that a client should not be forced to depend on methods it does not use. In other words, you should design interfaces that are specific to each client's needs, rather than creating a monolithic interface that includes methods for all clients. This principle helps to promote modularity and maintainability.&lt;br&gt;
In TypeScript, you can use interfaces to achieve the ISP. &lt;/p&gt;

&lt;p&gt;Here's an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Database.ts
export interface Database {
  connect(): void;
  disconnect(): void;
  query(sql: string): any[];
}

// SqliteDatabase.ts
import { Database } from './Database';

export class SqliteDatabase implements Database {
  public connect(): void {
    console.log('Connecting to SQLite database');
  }

  public disconnect(): void {
    console.log('Disconnecting from SQLite database');
  }

  public query(sql: string): any[] {
    console.log(`Executing SQL query: ${sql}`);
    return [];
  }
}

// RedisDatabase.ts
import { Database } from './Database';

export class RedisDatabase implements Database {
  public connect(): void {
    console.log('Connecting to Redis database');
  }

  public disconnect(): void {
    console.log('Disconnecting from Redis database');
  }

  public query(sql: string): any[] {
    throw new Error('Redis does not support SQL queries');
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;In this example, the &lt;code&gt;Database&lt;/code&gt; interface defines a contract for databases that can connect, disconnect, and execute SQL queries. The &lt;code&gt;SqliteDatabase&lt;/code&gt; and &lt;code&gt;RedisDatabase&lt;/code&gt; classes implement the &lt;code&gt;Database&lt;/code&gt; interface, providing their own implementations of the methods.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;By using interfaces and the ISP, we can create specific interfaces for each client's needs, rather than creating a monolithic interface that includes methods for all clients. This promotes modularity and maintainability, making the code more flexible and reusable.&lt;/p&gt;






&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Fifth :Dependency Inversion Principle (DIP)
&lt;/h3&gt;

&lt;p&gt;The DIP states that high-level modules should not depend on low-level modules. Instead, both should depend on abstractions. In other words, you should design your system so that high-level modules can be easily swapped out with other modules that implement the same abstractions. This principle helps to promote extensibility and maintainability.&lt;br&gt;
In TypeScript, you can use inversion of control (IoC) and dependency injection (DI) to achieve the DIP. &lt;/p&gt;

&lt;p&gt;Here's an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Logger.ts
export interface Logger {
  log(message: string): void;
}

// ConsoleLogger.ts
import { Logger } from './Logger';

export class ConsoleLogger implements Logger {
  public log(message: string): void {
    console.log(message);
  }
}

// Service.ts
import { Logger } from './Logger';

export class Service {
  private logger: Logger;

  constructor(logger: Logger) {
    this.logger = logger;
  }

  public doSomething(): void {
    this.logger.log('Doing something');
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;In this example, the &lt;code&gt;Logger&lt;/code&gt; interface defines a contract for loggers that can log messages. The &lt;code&gt;ConsoleLogger&lt;/code&gt; class implements the &lt;code&gt;Logger&lt;/code&gt; interface, providing its own implementation of the &lt;code&gt;log()&lt;/code&gt; method. The &lt;code&gt;Service&lt;/code&gt; class depends on the &lt;code&gt;Logger&lt;/code&gt; interface, rather than depending on a specific logger implementation. This allows us to easily swap out the &lt;code&gt;ConsoleLogger&lt;/code&gt; with another logger implementation that implements the same &lt;code&gt;Logger&lt;/code&gt; interface.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;By using IoC and DI, we can design our system so that high-level modules depend on abstractions rather than low-level modules, promoting extensibility and maintainability.&lt;/p&gt;




&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  So .. Who Wins?
&lt;/h2&gt;

&lt;p&gt;TypeScript has enhanced OOP capabilities than JavaScript, particularly when it comes to adhering to the SOLID principles.&lt;/p&gt;

&lt;p&gt;By adhering to these principles, you can create code that is more modular, maintainable, and robust. &lt;/p&gt;

&lt;p&gt;In addition to adhering to the SOLID principles, TypeScript also provides several key features that make it a powerful tool for building complex software systems. These features make TypeScript a powerful tool for building complex software systems, particularly those that rely heavily on OOP principles. &lt;/p&gt;

&lt;p&gt;By using TypeScript, you can create code that is more reliable, maintainable, and extensible, which can save you time and money in the long run. Moreover, TypeScript provides a powerful set of tools for creating and managing complex software systems, making it an excellent choice for building large-scale applications.&lt;/p&gt;




&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;h4&gt;
  
  
  Feel free to check out the references for more details and points of view
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.dotnettricks.com/learn/typescript/modules-namespaces"&gt;https://www.dotnettricks.com/learn/typescript/modules-namespaces&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.tutorialsteacher.com/typescript/typescript-namespace"&gt;https://www.tutorialsteacher.com/typescript/typescript-namespace&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.typescriptlang.org/docs/handbook/"&gt;https://www.typescriptlang.org/docs/handbook/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://mirone.me/a-complete-guide-to-typescript-decorator/"&gt;https://mirone.me/a-complete-guide-to-typescript-decorator/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.logrocket.com/typescript-mixins-examples-and-use-cases/"&gt;https://blog.logrocket.com/typescript-mixins-examples-and-use-cases/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://birdeatsbug.com/blog/object-oriented-programming-in-typescript"&gt;https://birdeatsbug.com/blog/object-oriented-programming-in-typescript&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/jafari-dev/oop-expert-with-typescript"&gt;https://github.com/jafari-dev/oop-expert-with-typescript&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://levelup.gitconnected.com/typescript-object-oriented-concepts-in-a-nutshell-cb2fdeeffe6e"&gt;https://levelup.gitconnected.com/typescript-object-oriented-concepts-in-a-nutshell-cb2fdeeffe6e&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/@aliafsah1988/how-to-properly-use-typescript-oop-and-solid-in-modern-web-applications-e4ef88761154"&gt;https://medium.com/@aliafsah1988/how-to-properly-use-typescript-oop-and-solid-in-modern-web-applications-e4ef88761154&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/@baruahd5/object-oriented-programming-oops-using-typescript-cdbffd7d0cf5"&gt;https://medium.com/@baruahd5/object-oriented-programming-oops-using-typescript-cdbffd7d0cf5&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/kevin_odongo35/object-oriented-programming-with-typescript-574o"&gt;https://dev.to/kevin_odongo35/object-oriented-programming-with-typescript-574o&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://betterprogramming.pub/understand-object-oriented-programming-with-typescript-c4ff8afa40d"&gt;https://betterprogramming.pub/understand-object-oriented-programming-with-typescript-c4ff8afa40d&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://css-tricks.com/the-flavors-of-object-oriented-programming-in-javascript/"&gt;https://css-tricks.com/the-flavors-of-object-oriented-programming-in-javascript/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.freecodecamp.org/news/object-oriented-programming-javascript/"&gt;https://www.freecodecamp.org/news/object-oriented-programming-javascript/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object-oriented_programming"&gt;https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object-oriented_programming&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://javascript.plainenglish.io/why-javascript-is-called-prototype-based-e9326562bf43"&gt;https://javascript.plainenglish.io/why-javascript-is-called-prototype-based-e9326562bf43&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.javatpoint.com/typescript-generics"&gt;https://www.javatpoint.com/typescript-generics&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.freecodecamp.org/news/typescript-generics-use-case-example/"&gt;https://www.freecodecamp.org/news/typescript-generics-use-case-example/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.javatpoint.com/javascript-oops-abstraction"&gt;https://www.javatpoint.com/javascript-oops-abstraction&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/swlh/typescript-doesnt-turn-javascript-into-a-class-based-oop-language-b31aae6114e3"&gt;https://medium.com/swlh/typescript-doesnt-turn-javascript-into-a-class-based-oop-language-b31aae6114e3&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.digitalocean.com/community/tutorials/how-to-use-classes-in-typescript"&gt;https://www.digitalocean.com/community/tutorials/how-to-use-classes-in-typescript&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/set"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/set&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects#defining_getters_and_setters"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects#defining_getters_and_setters&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/static"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/static&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Private_class_fields"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Private_class_fields&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.bitsrc.io/understanding-public-and-private-fields-in-javascript-class-a281bf930566"&gt;https://blog.bitsrc.io/understanding-public-and-private-fields-in-javascript-class-a281bf930566&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://connorsmyth.com/implementing-interface-in-javascript/"&gt;https://connorsmyth.com/implementing-interface-in-javascript/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Duck_typing"&gt;https://en.wikipedia.org/wiki/Duck_typing&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.typescripttutorial.net/typescript-tutorial/typescript-getters-setters/"&gt;https://www.typescripttutorial.net/typescript-tutorial/typescript-getters-setters/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.javascripttutorial.net/es6/javascript-getters-and-setters/"&gt;https://www.javascripttutorial.net/es6/javascript-getters-and-setters/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>typescript</category>
      <category>oop</category>
    </item>
  </channel>
</rss>
