TypeScript Answers

How to do method overloading in TypeScript?

Spread the love

To do method overloading in TypeScript, we can add multiple signatures for the same method.

For instance, we write

class TestClass {
  someMethod(stringParameter: string): void;
  someMethod(numberParameter: number, stringParameter: string): void;
  someMethod(stringOrNumberParameter: any, stringParameter?: string): void {
    if (stringOrNumberParameter && typeof stringOrNumberParameter == "number") {
    } else {

to add the someMethod in TestClass that has multiple signatures.

We specify all the possible signatures for the method by adding the parameters and their types for each one.

And then we check the values of the parameters with if statements before we do anything with them.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.