Languages
[Edit]
EN

TypeScript - function overloading

8 points
Created by:
Palus-Bear
1016

In this article, we would like to show how to overload function in TypeScript.

TypeScript supports function overloading syntax that lets to deflate multiple functions with the same name, different arguments and return types. It is required just to provide some functions declarations and one function definition.

Note: go to this article to know how to overload method in class.

Quick solution:

function someMethod(): ResultType;                          // <----- declaration
function someMethod(a: ArgType1): ResultType;               // <----- declaration
function someMethod(a: ArgType2, b: ArgType3): ResultType;  // <----- declaration
// ...

function someMethod(a?: any, b?: any): ResultType {         // <----- definition
    // ...                                                  // <----- definition
}                                                           // <----- definition

// ...

 

Practical example

function print(a: string): void;               // <----- declaration
function print(a: string, b: string): void;    // <----- declaration

function print(a: string, b?: string): void {  // <----- definition
    if (b) {                                   // <----- definition
        console.log(a + ' ' + b);              // <----- definition
    } else {                                   // <----- definition
        console.log(a);                        // <----- definition
    }                                          // <----- definition
}                                              // <----- definition

print('Hi!');
print('Hi', 'John!');

Output:

Hi!
Hi John!

 

Alternative solution

type Print = {                                           // <----- declaration
    (a: string): void;                                   // <----- declaration
    (a: string, b: string): void;                        // <----- declaration
};                                                       // <----- declaration

const print: Print = (a: string, b?: string): void => {  // <----- definition
    if (b) {                                             // <----- definition
        console.log(a + ' ' + b);                        // <----- definition
    } else {                                             // <----- definition
        console.log(a);                                  // <----- definition
    }                                                    // <----- definition
}                                                        // <----- definition

print('Hi!');
print('Hi', 'John!');

 

See also

  1. TypeScript - class methods overloading

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join