+1 (843) 212-6898+8801897661858
Whatsapp-colorCreated with Sketch.
sales@mediusware.comSchedule A Call
Logo
Company
Services
Hire Developers
Industries
Case StudySTART FREE TRIALicon

About

Who We Are

Our Team

Blogs

Women Empowerment

Career

CSR

Delivery Models

Engagement Model

Services

Software Development

Web Development

Mobile App Development

E-commerce Development

Software Development

Enterprise Solutions

UI/UX Design

API Integration

Technology

React.js

Vue.js

Angular js

Laravel

Android

Flutter

iOS

React Native

Hire Developers

Hire Mobile App Developers

Hire Front-end Developers

Hire Backend Developers

Hire E-commerce Developers

Hire Developers

Hire Android Developers

Hire iOS Developers

Hire Swift Developers

Hire Flutter Developers

Hire React Native Developers

Hire Django Developers

Hire Node.js Developers

Hire Laravel Developers

We shape the art of technology
Headquarter

Headquarter

1050 Johnnie Dodds Blvd Mount Pleasant South Carolina USA ,Zip- 29464

sales@mediusware.io

+1 843-212-7149

Bangladesh Office

Bangladesh Office

24/1, Taj Mahal Road, Shiya Masjid mor, Floor - 8th & 9th, Ring Road, 1207, Dhaka, Bangladesh

sales@mediusware.com

+8801897661858

© 2025 Mediusware. All Rights Reserved

Terms & ConditionsPrivacy Policy

Table of contents

  1. What is Method overloading?
  2. The HttpException Class
  3. How It Works
  4. Benefits of Method Overloading
Share This Blog !
Get the best of our content straight to your inbox!

Don’t worry, we don’t spam!

Understanding TypeScript Method Overloading: A Deep Dive with HttpException

Understanding TypeScript Method Overloading: A Deep Dive with HttpException image

Last Update: 27 Oct 2024

What is Method overloading?

Method overloading is a powerful feature in TypeScript that allows a single method to have multiple signatures. This enables us to define different ways of calling a method based on the number or types of arguments. Today, we'll explore this concept using a real-world example from the NestJS framework: the `HttpException` class.

The HttpException Class

Let's look at the `createBody` method from the `HttpException` class:

 

class HttpException {
  // ... other parts of the class ...

  public static createBody(
    nil: null | '',
    message: HttpExceptionBodyMessage,
    statusCode: number,
  ): HttpExceptionBody;

  public static createBody(
    message: HttpExceptionBodyMessage,
    error: string,
    statusCode: number,
  ): HttpExceptionBody;

  public static createBody<Body extends Record<string, unknown>>(
    custom: Body,
  ): Body;

  public static createBody<Body extends Record<string, unknown>>(
    arg0: null | HttpExceptionBodyMessage | Body,
    arg1?: HttpExceptionBodyMessage | string,
    statusCode?: number,
  ): HttpExceptionBody | Body {
    if (!arg0) {
      return {
        message: arg1,
        statusCode: statusCode,
      };
    }

    if (isString(arg0) || Array.isArray(arg0)) {
      return {
        message: arg0,
        error: arg1 as string,
        statusCode: statusCode,
      };
    }

    return arg0;
  }
}

Breaking Down the Overloads 

1. First Overload: 

createBody(nil: null | '', message: HttpExceptionBodyMessage, statusCode: number): HttpExceptionBody;

 

   This overload is used when you want to create a body with just a message and status code, without an error string.

2. Second Overload:

createBody(message: HttpExceptionBodyMessage, error: string, statusCode: number): HttpExceptionBody;

 

This overload allows creating a body with a message, an error string, and a status code.

3. Third Overload:

createBody<Body extends Record<string, unknown>>(custom: Body): Body;

 

This overload enables passing a custom body object, providing maximum flexibility.

4. Implementation:

 createBody<Body extends Record<string, unknown>>(
     arg0: null | HttpExceptionBodyMessage | Body,
     arg1?: HttpExceptionBodyMessage | string,
     statusCode?: number,
   ): HttpExceptionBody | Body

 

This is the actual implementation that handles all the above cases.

How It Works

The TypeScript compiler uses these overloads to determine the correct method signature based on the arguments provided when calling `createBody`. The implementation then uses type checking to construct the appropriate response body.

Usage Examples:

// Using the first overload
HttpException.createBody(null, "Not Found", 404);
// Result: { message: "Not Found", statusCode: 404 }

// Using the second overload
HttpException.createBody("Forbidden", "Access Denied", 403);
// Result: { message: "Forbidden", error: "Access Denied", statusCode: 403 }

// Using the third overload
HttpException.createBody({ custom: "error", code: 500 });
// Result: { custom: "error", code: 500 }

Benefits of Method Overloading

Flexibility: It allows a single method to handle different input scenarios.

Type Safety: TypeScript ensures that the correct types are used for each overload.

Code Reusability: A single implementation can handle multiple use cases.

Improved Readability: Overloads provide clear documentation of the different ways a method can be used.

Trendingblogs
The Software Development Life Cycle (SDLC): A Complete Guide for Software Success image

The Software Development Life Cycle (SDLC): A Complete Guide for Software Success

Zustand: A Lightweight State Management Library for React image

Zustand: A Lightweight State Management Library for React

From Bugs to Beauty: Integrating UI/UX Testing into Software Quality Assurance image

From Bugs to Beauty: Integrating UI/UX Testing into Software Quality Assurance

Why is a Gaming PC the Ultimate Tool for Both Gaming and Professional Development? image

Why is a Gaming PC the Ultimate Tool for Both Gaming and Professional Development?

Why React Native is the Best Choice for Cross-Platform Development image

Why React Native is the Best Choice for Cross-Platform Development

Let's Deep Dive Into Threads with Rust image

Let's Deep Dive Into Threads with Rust

Get the best of our content straight to your inbox!

Don’t worry, we don’t spam!

Frequently Asked Questions