Saturday 17 February 2024

What is Polymorphism in Typescript ?071

 What is Polymorphism in Typescript ?

Polymorphism in TypeScript, a core object-oriented programming concept, allows objects of different classes to be treated as instances of a common ancestor class or interface. This enables flexible, reusable code with dynamic behavior based on the actual object type at runtime.

Key aspects:

  • Subtyping: Subclasses are considered subtypes of their base classes, meaning they can be used wherever the base class type is expected.

  • Method overriding: Subclasses can override inherited methods to provide their own implementation, leading to different behaviors for the same method name when called on different object types.

  • Interfaces: Define contracts for expected behavior without implementation details, enabling polymorphism through implementing classes.

  • Dynamic dispatch: At runtime, the appropriate method implementation is chosen based on the actual object's type, even if the variable holding the object is typed to the common ancestor class or interface.


  • Code Reuse: Reduces code duplication by defining common behavior in the base class or interface and allowing specific implementations in subclasses.

  • Flexibility: Enables code to work with different object types without knowing their specific details, enhancing adaptability.

  • Open-Closed Principle: New subclasses can be added without modifying existing code that uses the common interface or base class.



interface Shape {
  area(): number;

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

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

  area(): number {
    return this.width * this.height;

class Circle implements Shape {
  radius: number;

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

  area(): number {
    return Math.PI * this.radius * this.radius;

// Polymorphic function using the Shape interface
function calculateTotalArea(shapes: Shape[]): number {
  let totalArea = 0;
  for (const shape of shapes) {
    totalArea += shape.area();
  return totalArea;

const shapes = [new Rectangle(5, 10), new Circle(4)];
const totalArea = calculateTotalArea(shapes);
console.log("Total area:", totalArea); // Output: 78.53981633974483

In this example:

  • Both Rectangle and Circle implement the Shape interface, providing polymorphic behavior for the calculateTotalArea function.

  • The function calculates the area correctly for each shape object based on its actual type, even though the array is typed to Shape[].


  • Use polymorphism thoughtfully to avoid overly complex inheritance hierarchies or confusing runtime behavior.

  • Prefer interfaces over concrete base classes for more flexible and decoupled polymorphism.

  • Consider alternatives like duck typing (checking object properties at runtime) in specific situations.

By effectively applying polymorphism in your TypeScript projects, you can write more adaptable, extensible, and reusable code, enhancing its maintainability and long-term value.




No comments:

Post a Comment

Note: only a member of this blog may post a comment.