parent
179bf366cc
commit
62905bda7a
3 changed files with 94 additions and 3 deletions
@ -1 +1,33 @@ |
|||||||
# Typing functions |
# 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) |
@ -1 +1,23 @@ |
|||||||
# Function overloading |
# 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) |
@ -1 +1,38 @@ |
|||||||
# Functions |
# 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) |
Loading…
Reference in new issue