-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.ts
More file actions
133 lines (89 loc) · 2.14 KB
/
Copy pathapp.ts
File metadata and controls
133 lines (89 loc) · 2.14 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
//String
let myName = 'Max';
//Number
let myAge = 27;
//boolean
let hasHobbies = false;
//assing types
let myRealage
myRealage = 27;
myRealage = '27';
let myRealage2: number;
myRealage2 = 37;
//Array of string
let hobbies = ["cooking", "Sport"];
console.log(typeof hobbies);
console.log(hobbies[0]);
//Array of any
let hobbies2: any[] = ["cooking2", "Sport2"];
hobbies2 = [100];
console.log(typeof hobbies);
console.log(hobbies[0]);
//tuples (array with mix type)
let address : [string, number ] = ["telaviv", 99]; //order is inportent
//Emun
enum Color{
Gray, //0
Green, //1
Blue //2
}
let myColor: Color = Color.Green;
console.log(myColor); // will print 1
//any
let car: any = "BMW";
console.log(car);
car = { brand: "BMW", model: 1988};
console.log(car);
//------------------------------------------------------------------------------------
//Functions
function returnMyMame():string {
return myName;
}
console.log(returnMyMame());
//void
function sayHello():void{
console.log("Hello");
}
//arguments type
function multiply(value1:number, value2:number) :number{
return value1*value2;
}
//function type
let myMultiply: (a:number, b:number) => number; //able to defind which type of function may hold
myMultiply = multiply;
console.log(myMultiply(5,10));
//Objects
//In objects the order my be change
let userData: {name: string, age: number} ={
name: "bebe",
age: 60
}
//complex object
let complex: {data:number[], output:(all:boolean) => number[]} ={
data:[100,2,5],
output: function (all:boolean): number[]{
return this.data;
}
}
//type alias
type Complex = {data:number[], output:(all:boolean) => number[]};
let complex2: Complex ={
data:[100,2,5],
output: function (all:boolean): number[]{
return this.data;
}
}
//union types
let myRealRealAge: number | string = 24; //Number or String
myRealRealAge = "27";
myRealRealAge = 27;
// check types
let finalValue = 30;
if (typeof finalValue == "number"){
console.log("the type is number");
}
//Nullable types
let canBeNull: number | null = 12;
canBeNull = null;
let alsoCanBeNull;
alsoCanBeNull = null;