HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / TypeScript / TypeScript Function or Method Overloading

TypeScript Function or Method Overloading

In TypeScript, function overloading, or method overloading, is the ability to create multiple methods with the same name and a different number of parameters or types.

So essentially, method overloading is allowed when –

  1. Function name is same
  2. Number of parameters are different in each overload
  3. If number of parameters is same the their type must be different
  4. All overloads must have same return type

Also note that, function implementation must be compatible with all the overloaded signatures. It should always be the last in the list, and take the any type or a union type as the type of its parameters.

Function Overloading Example

Suppose we are creating a function which will return the Employee type based on different parameters such as id, email, name or their combination. let’s create overloaded functions for this requirement.

class Employee {}

function getEmployee(id: number): Employee; 					//Overload 1
function getEmployee(email: string): Employee; 					//Overload 2
function getEmployee(email: number, name: string): Employee; 	//Overload 3

//function getEmployee(name: string): Employee; 				//Error - Conflict with Overload 2

//Implement the function
function getEmployee (paramOne: string | number, paramTwo?: string ): Employee { 
	
	let employee: Employee;

    if( typeof paramOne === 'number') {
    	//Logic for overload 1
	} else if( typeof paramTwo != 'undefined') {
		//Logic for overload 3
	} else {
		//Logic for overload 2
	}

	return employee;
} 

Drop me your questions in comments section.

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Alan

    March 4, 2020

    Will intelliSense take advantage of it? I mean: If I start to type getEmployee(323, will the intelliSense show me the version which takes a number as param?

  2. Alain

    November 14, 2019

    I think you meant
    function getEmployee(email: string, name: string)
    otherwise:
    if ( typeof paramOne === ‘number’) {
    //Logic for overload 1
    }
    could be used for case 3 as well…

  3. Basavaraj

    October 1, 2018

    how overload methods within the class

    • Lokesh Gupta

      October 1, 2018

      Method overloading happen within class. You override a method in child class.

Comments are closed on this article!

Search Tutorials

TypeScript Tutorial

  • TypeScript – Introduction
  • TypeScript – Types
  • TypeScript – Union Types
  • TypeScript – String Literal Types
  • TypeScript – var, let and const
  • TypeScript – Template Strings
  • TypeScript – Arithmetic Operators
  • TypeScript – Logical Operators
  • TypeScript – Comparison Operators
  • TypeScript – ‘for…of’ Loop
  • TypeScript – Spread Operator
  • TypeScript – Arrays
  • TypeScript – Enums
  • TypeScript – Map
  • TypeScript – Set
  • TypeScript – Functions
  • TypeScript – Function Overloading
  • TypeScript – Transpiler
  • TypeScript – Truthy and falsy
  • TypeScript – == vs ===
  • TypeScript – undefined vs null
  • TypeScript – Variable Hoisting
  • TypeScript – tsconfig.json

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces