Want to know how to do function overloading in TypeScript? Use the following TypeScript code and execute it to see the output. In this example, We have shared TypeScript concept for function overloading.
1 2 3 4 5 6 7 8 9 10 11 12 13 | function add(a:string, b:string):string; function add(a:number, b:number): number; function add(a: any, b:any): any { return a + b; } // returns "Hello Steve" add("Hello ", "Steve"); // returns 30 add(10, 20); |
Another example of Function overloading typescript.
1 2 3 4 5 6 7 8 9 10 11 | //Compiler Error: Duplicate function implementation. function display(a:string, b:string):void { console.log(a + b); } //Compiler Error: Duplicate function implementation. function display(a:number): void { console.log(a); } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.