-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctionParametersAnnotations
More file actions
198 lines (138 loc) · 5.92 KB
/
Copy pathFunctionParametersAnnotations
File metadata and controls
198 lines (138 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
## Function Parameters Annotations :-
- Function Parameters annotations in Typescript are used to specify the expected types of the paramters that function takes.
- here is the example of the function paramters annotations
```ts
// Regular Function Annotations
function addOne(num: number) {
return num + 1;
}
// calling the function and storing to the variable
const result = addOne(3);
console.log(result);
// Arrow Function Annotations
const double = (x: number, y: number) => {
return x * y;
};
// calling the function and storing to the variable with the number data type arguments passing :-
const res = double(2, 10);
console.log(res);
```
- NOTE :-
Also notice, Typescript will gives you warning if you provide more or less arguments,
then you specify in your parameters area
for an example , from the above `double` code function
```ts
double(2, 10, 20); // 👈 warning or error
// error like :- expected 2 arguments , but got 3.
```
## Default Params Value :-
- here is the example code for it , how we assign the type to default value for the function.
```ts
function greet(person: string = "Anonymous") {
return `Hello ${person}`;
}
const res = greet();
console.log(res);
```
- so this will give the output as `Hello Anonymous`
- as the function have default value and we are not passing any arguments, so it will use the default value.
- but that default value we have `annoted the type` as `string`.
- so that any other data type passed will not work.
- and if we want to override the default value passed in the function, we would do like this 👇
```ts
function greetByName(person: string = "Anonymous") {
return `Hello ${person}`;
}
// calling the function and storing to the variable,
// string data type being passed to function call.
const resByName = greetByName("Aman Yadav");
console.log(resByName);
```
- so this will give the output as `Hello Aman Yadav`
- as the function have default value and we are now passing the arguments, so it will override the default value.
- but that overriden value we have `annoted the type` as `string`.
- so that any other data type passed will not work.
- and it will gives us an error that type string is not assinable to type number, when we try to pass the number data type arguments.
## Return Annotations (Regular Function)
- now we will learn about `return value` for a `function annotations` in typescript.
- here is the example of it 👇
```ts
// Regular function
function double1(x: number): number {
return x * x;
}
const result = double1(5);
// it will return only number and answer will be 25
console.log(result);
```
- in that above function outside of the parameter area that is this -> `(x:number)` we will write `: number` to return a specific type of value.
## Return Annotations (Arrow Function)
- now here we will learn how to annotate the `return value` for `arrow functions` in typescript.
- here is the example of it 👇
```ts
// Using Arrow Function
const double2 = (y: number): number => y * y;
// it will return only number and answer will be 9
const result2 = double2(3);
console.log(result2);
```
- in that above function outside of the parameter area that is this -> `(y:number)` we will write `: number` to return a specific type of value.
## Void In Typescript
- `Void` is a type that represents the `absence of any value`.
- it is often used as `return type` for a `functions that do not return any value`.
- here is the example code to understand better 👇
```ts
function printMessage(message: string): void {
console.log(`This is my message :- ${message}`);
// ERROR 👇
return message;
}
printMessage("Hello How Are You ?");
```
- so for better clearity , what we are doing is that suppose for an example we are not defining any return type like `string`, `number` and not returning any value from the function
- here is the above code without return statment and return annotations 👇
```ts
function printMessage1(message: string) {
console.log(`This is my message :- ${message}`);
}
printMessage1("Hii, How are you ?");
```
- so typescript will implicitly annoted the function return value as `void`.
- as we are not returning any value from the function.
- and if we provide a return statement to the function then it will automatically get the return type of the function that is for above example `printMessage` function is string.
- and if still we have written `:void` for a function return annotations and we are returning then it will gives us an compile error that is `Type 'string' is not assignable to type 'void'.`
- on the return statment line itself
- u should `avoid to use void` as functions must return some value but for void case it can be used in some cases.
- it may be run the code correctly but it is a bad practise to do.
## Never
- This is the keyword we can simply say as father, mother , grandfather or whatever you want to call of `any keyword`😅
- The `never keyword` is used to indicate that a `function will not return anything`, or that `variable can never have a value`.
- 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.
<strong>We are using Never keyword for following reasons 👇</strong>
1. A function that always throws an error.
2. A function that has an infinite loop.
3. A variable that can never have a value.
Examples of the never keyword 👇
1. A function that always throws an error.
```ts
function throwErrorMsg(message: string): never {
throw new Error(message);
}
```
2. A function that has an infinite loop.
```ts
function infiniteLoop(): never {
while (true) {}
}
```
3. A variable that can never have a value.
```ts
// we are declearing variable x and annoted with never keyword
let x: never;
function neverReturns(): never {
while (true) {}
}
x = neverReturns();
```
- this `x = neverReturns()` line will cause a `compile time error` because the function never returns.