Skip to content

Commit 9c4ac1b

Browse files
committed
Functions
1 parent 6207e30 commit 9c4ac1b

10 files changed

+171
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Regular Func
2+
function addOne(num: number) {
3+
return num + 1;
4+
}
5+
6+
const result = addOne(3);
7+
console.log(result);
8+
9+
// Arrow Func Annotations
10+
const double = (x: number, y: number) => x * y;
11+
const res = double(2, 10);
12+
console.log(res);
13+
14+
// Also notice, TypeScript will gives you warning if you provide more or less arguments then you specifiy in your parameters area.
15+
16+
// double(2, 10, 20); // 👈 Warning

3. Functions/2. Default-Parameters.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function greet(person: string = "Anonymous") {
2+
return `Hello ${person}`;
3+
}
4+
5+
const res = greet();
6+
console.log(res);

3. Functions/3. Return-Annotations.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Regular Function
2+
function double1(x: number): number {
3+
return x * x;
4+
}
5+
6+
const res = double1(2);
7+
console.log(res);
8+
9+
// Using Arrow Functions
10+
const double2 = (x: number): number => x * x;
11+
const res2 = double2(2);
12+
console.log(res2);

3. Functions/4. Void.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function printMessage(message: string): void {
2+
console.log(`This is my message: ${message}`);
3+
// ERROR 👇
4+
// return message;
5+
}
6+
7+
printMessage("Hello How Are You?");

3. Functions/5. Never.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// 1.
2+
function throwError(msg: string): never {
3+
throw new Error(msg);
4+
}
5+
6+
// 2.
7+
function infiniteLoop(): never {
8+
while (true) {}
9+
}
10+
11+
// 3.
12+
let x: never;
13+
14+
function neverReturns(): never {
15+
while (true) {}
16+
}
17+
18+
x = neverReturns(); // This line will cause a compile-time error because the function never returns
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Function Parameter Annotations
2+
3+
Function parameter annotations in TypeScript are used to specify the expected types of the parameters that a function takes.
4+
5+
Here's an example:
6+
7+
```ts
8+
function greet(name: string) {
9+
console.log(`Hello, ${name}!`);
10+
}
11+
```
12+
13+
In this example, we're declaring a function greet with a single parameter name. The name parameter is annotated with the type string, which means that it can only accept string values.
14+
15+
```ts
16+
greet(42); // Error: Argument of type 'number' is not assignable to parameter of type 'string'.
17+
```
18+
19+
If we call the greet function with a non-string value, TypeScript will show an error:
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Default Parameters
2+
3+
Default parameters in TypeScript allow you to specify a default value for a function parameter if one is not provided when the function is called.
4+
5+
Here's an example:
6+
7+
```ts
8+
function greet(name: string = "world") {
9+
console.log(`Hello, ${name}!`);
10+
}
11+
12+
greet(); // Output: "Hello, world!"
13+
greet("HuXn"); // Output: "Hello, HuXn!"
14+
```
15+
16+
In this example, we declare a function greet that takes a single parameter name, which has a default value of 'world'. If the name parameter is not provided when the function is called, it will default to 'world'.
17+
18+
When we call greet() without any arguments, it outputs "Hello, world!". When we call greet('HuXn'), it outputs "Hello, HuXn!".
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Function Return Annotations
2+
3+
Function return type annotations in TypeScript are used to specify the expected return type of a function.
4+
5+
Here's an example:
6+
7+
```ts
8+
function add(a: number, b: number): number {
9+
return a + b;
10+
}
11+
```
12+
13+
In this example, we're declaring a function add that takes two number parameters a and b. The function is annotated with a return type of number, which means that it must return a numeric value.
14+
15+
If the function doesn't return a value that matches the specified return type, TypeScript will show an error:
16+
17+
```ts
18+
function add(a: number, b: number): number {
19+
return "hello"; // Error: Type 'string' is not assignable to type 'number'.
20+
}
21+
```

3. Functions/Learn/4. Void.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Void In TypeScript
2+
3+
In TypeScript, void is a type that represents the absence of any value. It is often used as the return type for functions that do not return a value.
4+
5+
Here's an example:
6+
7+
```ts
8+
function logMessage(message: string): void {
9+
console.log(`Message: ${message}`);
10+
}
11+
```
12+
13+
In this example, the function logMessage takes in a message parameter of type string, logs it to the console, but does not return any value. Therefore, its return type is void.

3. Functions/Learn/5. Never.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Never
2+
3+
The never keyword is used to indicate that a function will not return anything, or that a variable can never have a value.
4+
5+
Here are some common use cases for the never type:
6+
7+
1. A function that always throws an error:
8+
9+
```ts
10+
function throwError(msg: string): never {
11+
throw new Error(msg);
12+
}
13+
```
14+
15+
The above function takes in a message of type string, throws an error with that message, and never returns anything. The return type of this function is never.
16+
17+
2. A function that has an infinite loop:
18+
19+
```ts
20+
function infiniteLoop(): never {
21+
while (true) {}
22+
}
23+
```
24+
25+
The above function has an infinite loop, so it will never return anything. Its return type is also never.
26+
27+
3. A variable that can never have a value:
28+
29+
```ts
30+
let x: never;
31+
32+
function neverReturns(): never {
33+
while (true) {}
34+
}
35+
36+
x = neverReturns(); // This line will cause a compile-time error because the function never returns
37+
```
38+
39+
In this example, the variable x is declared as type never. Since it is not assigned any value, it cannot have a value. If we try to assign it a value using a function that never returns (like neverReturns()), TypeScript will raise a compile-time error.
40+
41+
The never type is useful for indicating that certain code paths should never be reached, or that certain values are impossible. It can help catch errors at compile-time instead of runtime.

0 commit comments

Comments
 (0)