diff --git a/src/roadmaps/typescript/content/106-functions/100-typing-functions.md b/src/roadmaps/typescript/content/106-functions/100-typing-functions.md index c22695642..b24ddddd1 100644 --- a/src/roadmaps/typescript/content/106-functions/100-typing-functions.md +++ b/src/roadmaps/typescript/content/106-functions/100-typing-functions.md @@ -1 +1,33 @@ -# Typing functions \ No newline at end of file +# Typing Functions + +In TypeScript, functions can be typed in a few different ways to indicate the input parameters and return type of the function. + +1. Function declaration with types: + + ``` + function add(a: number, b: number): number { + return a + b; + } + ``` + +2. Arrow function with types: + + ``` + const multiply = (a: number, b: number): number => { + return a * b; + }; + ``` + +3. Function type: + + ``` + let divide: (a: number, b: number) => number; + divide = (a, b) => { + return a / b; + }; + ``` + +Learn more from the following links: + +- [More on Functions](typescriptlang.org/docs/handbook/2/functions.html) +- [TypeScript Basics - Typing with functions](https://www.youtube.com/watch?v=do_8hnj45zg) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/106-functions/101-function-overloading.md b/src/roadmaps/typescript/content/106-functions/101-function-overloading.md index 0adb534b7..35cf2114b 100644 --- a/src/roadmaps/typescript/content/106-functions/101-function-overloading.md +++ b/src/roadmaps/typescript/content/106-functions/101-function-overloading.md @@ -1 +1,23 @@ -# Function overloading \ No newline at end of file +# Function Overloading + +Function Overloading in TypeScript allows multiple functions with the same name but with different parameters to be defined. The correct function to call is determined based on the number, type, and order of the arguments passed to the function at runtime. + +For example: + + ``` + function add(a: number, b: number): number; + function add(a: string, b: string): string; + + function add(a: any, b: any): any { + return a + b; + } + + console.log(add(1, 2)); // 3 + console.log(add("Hello", " World")); // "Hello World" + ``` + + +Learn more from the following links: + +- [TypeScript - Function Overloading](https://www.tutorialsteacher.com/typescript/function-overloading) +- [Function Overloads](https://www.typescriptlang.org/docs/handbook/2/functions.html#function-overloads) \ No newline at end of file diff --git a/src/roadmaps/typescript/content/106-functions/index.md b/src/roadmaps/typescript/content/106-functions/index.md index cebcc2697..76a8d4f9e 100644 --- a/src/roadmaps/typescript/content/106-functions/index.md +++ b/src/roadmaps/typescript/content/106-functions/index.md @@ -1 +1,38 @@ -# Functions \ No newline at end of file +# Functions + +Functions are a core building block in TypeScript. Functions allow you to wrap a piece of code and reuse it multiple times. Functions in TypeScript can be either declared using function declaration syntax or function expression syntax. + +1. Function Declaration Syntax: + + ``` + function name(param1: type1, param2: type2, ...): returnType { + // function body + return value; + } + ``` + +2. Function Expression Syntax: + + ``` + let name: (param1: type1, param2: type2, ...) => returnType = + function(param1: type1, param2: type2, ...): returnType { + // function body + return value; + }; + ``` + +For example: + + ``` + function add(a: number, b: number): number { + return a + b; + } + let result = add(1, 2); + console.log(result); // 3 + ``` + +Learn more from the following links: + +- [Functions](https://www.typescriptlang.org/docs/handbook/functions.html) +- [TypeScript Functions](https://www.w3schools.com/typescript/typescript_functions.php) +- [TypeScript - functions](youtube.com/watch?v=mblaKPWM9NU) \ No newline at end of file