TypeScript Function OverloadingFunction overloading is a mechanism or ability to create multiple methods with the same name but different parameter types and return type. However, it can have the same number of parameters. Function overloading is also known as method overloading. The Function/Method overloading is allowed when:
Suppose we have to perform multiplication of the numbers, which has a different number of parameters. We write the two methods such as mul_a(number, number) for two parameters, and mul_b(number, number, number) for three parameters. Now, it may be difficult for us as well as other programmers to understand the behavior of the method because its name differs. In that case, we need to use function overloading, which increases the readability of the program. Advantage of function overloading
Example In the above example:
After compiling the above TypeScript program, we will get the following JavaScript code. Output: Function overloading in a classThe following example helps to understand the use of method overloading in a class. After compiling the above TypeScript program, we will get the following JavaScript code. Output: Function overloading with a different number of parameters and different types along with the same function name is not supported. ExampleNext TopicTypeScript Function Parameter |