Skip to content

Commit d54f914

Browse files
committed
done with chapter 6
1 parent 6c0aa6e commit d54f914

File tree

6 files changed

+179
-0
lines changed

6 files changed

+179
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,11 @@
4242
- [Section 5.2: How to get all enum values](/book_pages/chapter5/section5.2.md)
4343
- [Section 5.3: Extending enums without custom enum implementation](/book_pages/chapter5/section5.3.md)
4444
- [Section 5.4: Custom enum implementation: extends for enums](/book_pages/chapter5/section5.4.md)
45+
46+
* Chapter 6: Functions
47+
48+
- [Section 6.1: Optional and Default Parameters](/book_pages/chapter6/section6.1.md)
49+
- [Section 6.2: Function as a parameter](/book_pages/chapter6/section6.2.md)
50+
- [Section 6.3: Functions with Union Types](/book_pages/chapter6/section6.3.md)
51+
- [Section 6.4: Types of Functions](/book_pages/chapter6/section6.4.md)
52+

book_pages/chapter6/section6.1.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Section 6.1: Optional and Default Parameters
2+
3+
### Optional Parameters
4+
5+
In TypeScript, every parameter is assumed to be required by the function. You can add
6+
a `?` at the end of a parameter name to set it as optional.
7+
8+
For example, the lastName parameter of this function is optional:
9+
```ts
10+
function buildName(firstName: string, lastName?: string) {
11+
// ...
12+
}
13+
```
14+
15+
Optional parameters **must come after** all non-optional parameters:
16+
```ts
17+
function buildName(firstName?: string, lastName: string) // Invalid
18+
```
19+
20+
### Default Parameters
21+
22+
If the user passes undefined or doesn't specify an argument, the default value will be
23+
assigned. These are called default-initialized parameters.
24+
For example, `"Smith" is the default value` for the lastName parameter.
25+
26+
```ts
27+
function buildName(firstName: string, lastName = "Smith") {
28+
// ...
29+
}
30+
31+
buildName('foo', 'bar'); // firstName == 'foo', lastName == 'bar'
32+
buildName('foo'); // firstName == 'foo', lastName == 'Smith'
33+
buildName('foo', undefined); // firstName == 'foo', lastName == 'Smith'
34+
```

book_pages/chapter6/section6.2.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Section 6.2: Function as a parameter
2+
3+
### Suppose we want to receive a function as a parameter, we can do it like this:
4+
```ts
5+
function foo(otherFunc: Function): void {
6+
...
7+
}
8+
```
9+
10+
### If we want to receive a constructor as a parameter:
11+
```ts
12+
function foo(constructorFunc: { new() }) {
13+
new constructorFunc();
14+
}
15+
16+
function foo(constructorWithParamsFunc: { new(num: number) }) {
17+
new constructorWithParamsFunc(1);
18+
}
19+
```
20+
21+
### Or to make it easier to read we can define an interface describing the constructor:
22+
```ts
23+
interface IConstructor {
24+
new();
25+
}
26+
27+
function foo(contructorFunc: IConstructor) {
28+
new constructorFunc();
29+
}
30+
```
31+
### Or with parameters:
32+
```ts
33+
interface INumberConstructor {
34+
new(num: number);
35+
}
36+
37+
function foo(contructorFunc: INumberConstructor) {
38+
new contructorFunc(1);
39+
}
40+
```
41+
42+
### Even with generics:
43+
```ts
44+
interface ITConstructor<T, U> {
45+
new(item: T): U;
46+
}
47+
48+
function foo<T, U>(contructorFunc: ITConstructor<T, U>, item: T): U {
49+
return new contructorFunc(item);
50+
}
51+
```
52+
53+
### If we want to receive a simple function and not a constructor it's almost the same:
54+
```ts
55+
function foo(func: { (): void }) {
56+
func();
57+
}
58+
59+
function foo(constructorWithParamsFunc: { (num: number): void }) {
60+
new constructorWithParamsFunc(1);
61+
}
62+
```
63+
### Or to make it easier to read we can define an interface describing the function:
64+
```ts
65+
interface IFunction {
66+
(): void;
67+
}
68+
69+
function foo(func: IFunction ) {
70+
func();
71+
}
72+
```
73+
74+
### Or with parameters:
75+
```ts
76+
interface INumberFunction {
77+
(num: number): string;
78+
}
79+
80+
function foo(func: INumberFunction ) {
81+
func(1);
82+
}
83+
```
84+
85+
### Even with generics:
86+
```ts
87+
interface ITFunc<T, U> {
88+
(item: T): U;
89+
}
90+
91+
function foo<T, U>(contructorFunc: ITFunc<T, U>, item: T): U {
92+
return func(item);
93+
}
94+
```

book_pages/chapter6/section6.3.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Section 6.3: Functions with Union Types
2+
3+
### A TypeScript function can take in parameters of multiple, predefined types using union types.
4+
```ts
5+
function whatTime(hour:number|string, minute:number|string):string{
6+
return hour+':'+minute;
7+
}
8+
9+
whatTime(1,30) //'1:30'
10+
whatTime('1',30) //'1:30'
11+
whatTime(1,'30') //'1:30'
12+
whatTime('1','30') //'1:30'
13+
```
14+
15+
TypeScript treats these parameters as a single type that is a union of the other types, so your function must be able to handle parameters of any type that is in the union.
16+
```ts
17+
function addTen(start:number|string):number{
18+
if(typeof number === 'string'){
19+
return parseInt(number)+10;
20+
}else{
21+
else return number+10;
22+
}
23+
}
24+
```

book_pages/chapter6/section6.4.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Section 6.4: Types of Functions
2+
3+
### Named functions
4+
```ts
5+
function multiply(a, b) {
6+
return a * b;
7+
}
8+
```
9+
10+
### Anonymous functions
11+
```ts
12+
let multiply = function(a, b) { return a * b; };
13+
```
14+
15+
### Lambda / arrow functions
16+
```ts
17+
let multiply = (a, b) => { return a * b; };
18+
```

myTs.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('Welcome to TypeScript Professionals');

0 commit comments

Comments
 (0)