-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
214 lines (171 loc) · 3.95 KB
/
app.ts
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// ==========================
// TYPES
// ==========================
// string
let myName: string = 'Isak';
// myName = 28;
// number
let myAge: number = 27.5;
// myAge = "Max";
// boolean
let hasHobbies: boolean = false;
// hasHobbies = 1;
// assign types
let myRealAge: number;
myRealAge = 27;
// myRealAge = "27";
// array
let hobbies: any[] = ["Cooking", "Sports"];
hobbies = [100];
// hobbies = 100;
// tuples - typescript
let address: [string, number] = ["Yxstabacken 639", 99];
// enum - typescript
enum Color {
Gray, // 0
Green = 100, // 1
Blue // 2
}
let myColor: Color = Color.Blue;
console.log(myColor)
// any - avoid using this type!
let car: any = "BMW";
console.log(car);
car = { brand: "BMW", series: 3};
console.log(car);
// ======================
// FUNCTIONS
// ======================
function returnMyName(): string {
return myName;
}
console.log(returnMyName());
function sayHello(): void {
console.log("Hello");
//return myName; // by using void, the function should not return a value, if it does TypeScript return a value.
}
function multiply(value1: number, value2: number): number {
return value1 * value2;
}
//console.log(multiply(2, "Isak"));
console.log(multiply(2,2));
// function types
let myMultiply: (val1: number, val2: number) => number;
// myMultiply = sayHello;
// myMultiply();
myMultiply = multiply;
// myMultiply();
console.log(myMultiply(5, 2));
// ====================
// OBJECTS
// ====================
let userData: {name: string, age: number, job: string } = {
name: "Isak",
age: 34,
job: "teacher"
};
// userData = {};
/*
userData = {
a: "Hello",
b: 22
};
*/
// complex object
let complex: {data: any[], output: (all: boolean) => number[]} = {
data: [100,3.55, 10],
output: function (all: boolean): number[] {
return this.data
}
};
// complex = {};
// type alias
type Complex = {data: any[], output: (all: boolean) => number[]};
let complex2: Complex = {
data: [100,3.55, 10],
output: function (all: boolean): number[] {
return this.data
}
};
// union types
let myRealRealAge: number | string = 27;
myRealRealAge = "27";
// myRealRealAge = true;
// check types
let finalValue = 10;
if (typeof finalValue == "number") {
console.log("Final value is a number");
}
// never type
function neverReturns():never {
throw new Error("An error!");
}
// Nullable Types
let canBeNull: number | null = 12;
canBeNull = null;
let canAlsoBeNull;
canAlsoBeNull = null;
let canThisBeAny = null;
// canThisBeAny = 12;
// ======================================
// Understanding the TypeScript Compiler
// ======================================
let myName2: string = "Isak";
let myAge2: number = 27;
let anything;
anything = 12;
/*
function controlMe(isTrue: boolean) {
let result: number;
if (isTrue) {
result = 12;
}
return result;
}
*/
function controlMe(isTrue: boolean, somethingElse: boolean) {
let result: number;
if (isTrue) {
result = 12;
}
result = 33;
return result;
}
// ===============================
// let and const variables in ES6
// ===============================
// var creates a global scope
// let variable creates a block scope
let variable = "test";
console.log(variable);
variable = "Another value";
console.log(variable);
const maxLevels = 100;
console.log(maxLevels);
// maxLevels = 99; // Won't work
// Block scope
function reset() {
// console.log(variable);
let variable = null;
console.log(variable);
}
reset();
console.log(variable);
// =====================
// Arrow Functions - E6
// =====================
// common function
console.log("Arrow functions");
const addNumbers = function(number1: number, number2: number): number {
return number1 + number2;
};
console.log(addNumbers(10, 3));
// arrow functions
const multiplyNumbers = (number1: number, number2: number) => number1 + number2;
console.log(multiplyNumbers(10,3));
const greet = () => {
console.log("Hello!");
}
greet();
const greetFriend = friend => console.log(friend);
greetFriend("Isak");