Skip to content

Commit 582e383

Browse files
committed
Added episode 7.
1 parent 0121511 commit 582e383

File tree

4 files changed

+155
-6
lines changed

4 files changed

+155
-6
lines changed

README.md

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,39 @@
1919
## Typescript tutorial examples
2020

2121

22-
- **1. hello world** (function helo world)
22+
- **1. Episode**
23+
24+
- function helo world
2325

2426
**output** :
2527
```shell
2628
Hello, World!
2729
```
2830

29-
- **2. first class** (simple class)
31+
- **2. Episode**
32+
33+
- first class
3034

3135
**output** :
3236
```shell
3337
elephant object = [object Object]
3438
```
3539

36-
- **3. bigger class** : (getters, setters, constructor with params, access keywords, )
40+
- **3. Episode**
41+
42+
- bigger class with getters, setters, constructor with params, access keywords
3743

3844
**output** :
3945
```shell
4046
elephant => name = elephant 2, age = 10, vaccination = true
4147
```
4248

43-
- **4. more constructors and more methods** : trick how you can have overloaded methods (technicaly still one method)
49+
- **4. Episode**
50+
51+
- trick how you can have overloaded methods (technicaly still one method)
4452
and multiple constructors (technically still one constructor) and lambda example.
4553

54+
4655
**output** :
4756
```shell
4857
tiger1 => name = Tiger1, age = undefined, vaccination = undefined
@@ -62,15 +71,23 @@ elephant => name = elephant 2, age = 10, vaccination = true
6271
10
6372
```
6473

65-
- **5. Interface and enum example**.
74+
- **5. Episode**
75+
76+
- interface
77+
- enum
6678

6779
**output** :
6880
```shell
6981
=> name = Audi R8 => speed = 230 => price = 3500 => sport = true
7082
=> name = BMW => speed = 220 => price = 4500 => sport = undefined
7183
```
7284

73-
- **6. Inheritance, abstract class, toString, instanceof example**.
85+
- **6. Episode**
86+
87+
- inheritance
88+
- abstract class
89+
- toString
90+
- instanceof
7491

7592
**output** :
7693
```shell
@@ -83,6 +100,28 @@ Vehicle = [ name : My Ford , sport : false , speed : 190 , price : 3000]
83100
```
84101
85102
103+
- **7. Episode **
104+
105+
- usage of type keyword
106+
- object literals
107+
- varargs
108+
- Function type
109+
110+
111+
**output** :
112+
```shell
113+
hello world
114+
one
115+
two
116+
one
117+
8
118+
2
119+
20
120+
9
121+
2
122+
```
123+
124+
86125
87126
## Prerequisites
88127

build/7-varargs-and-function-type/main.js

Lines changed: 49 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/7-varargs-and-function-type/main.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// narrow with interface
2+
interface SquareInterface {
3+
(x : number) : number
4+
}
5+
6+
class Lecture7 {
7+
8+
// function type
9+
hello : Function = (input : string) => {
10+
console.log("hello " + input)
11+
return "hello " + input
12+
}
13+
14+
// varargs example
15+
varargs(...objects : string[]) : void {
16+
for (let i = 0; i < objects.length; i++) {
17+
console.log(objects[i])
18+
}
19+
}
20+
21+
// object literal like parameter in function
22+
squareIt = function(rect : {h : number, w : number}) {
23+
return rect.h * rect.w;
24+
}
25+
26+
// narrow version of squareIt
27+
squareItNarrow = (rect : {h : number, w : number}) => rect.h * rect.w
28+
29+
squareItNarrowLog = (rect : {h : number, w : number}) => {
30+
let result : number = rect.h * rect.w
31+
console.log(result)
32+
return result
33+
}
34+
35+
squareItNarrowInt : SquareInterface = (num) => 4 * num
36+
}
37+
38+
// executable
39+
let lecture7 : Lecture7 = new Lecture7()
40+
lecture7.hello("world")
41+
lecture7.varargs("one", "two")
42+
lecture7.varargs("one")
43+
44+
let result : number = lecture7.squareItNarrowInt(2)
45+
console.log(result)
46+
47+
let result2 : number = lecture7.squareIt({h : 1, w : 2})
48+
console.log(result2)
49+
50+
let result3 : number = lecture7.squareItNarrow({h : 4, w : 5})
51+
console.log(result3)
52+
53+
// typed object literal obj
54+
let obj : {h : number, w : number} = {h : 3, w : 3}
55+
lecture7.squareItNarrowLog(obj)
56+
57+
// declaring new Object type Obj
58+
type Obj = {h : number, w : number}
59+
let o : Obj = {h : 1, w :2}
60+
lecture7.squareItNarrowLog(o)

0 commit comments

Comments
 (0)