├── README.md ├── chapter3 ├── backup_chapter3.ts ├── chapter3.js └── chapter3.ts ├── docs ├── lesson1 │ ├── lesson1.html │ ├── lesson1.js │ ├── lesson1VersionTS.js │ └── lesson1VersionTS.ts ├── lesson2 │ └── note.txt ├── lesson3 │ ├── lesson3.html │ ├── lesson3.js │ └── lesson3.ts ├── lesson4 │ ├── lesson4.ts │ └── lesson4JS.js ├── lesson5 │ ├── lesson5.js │ └── lesson5.ts ├── lesson6 │ ├── Lesson6.js │ └── Lesson6.ts └── lesson7 │ ├── lesson7.js │ └── lesson7.ts ├── index.html ├── lesson.js └── projects ├── lesson10 ├── lesson10.js └── lesson10.ts ├── lesson11 ├── lesson11.js └── lesson11.ts ├── lesson12 ├── lesson12.js └── lesson12.ts ├── lesson13 └── lesson13.ts ├── lesson14 └── lesson14.ts ├── lesson15 ├── lesson15.js └── lesson15.ts ├── lesson16 └── lesson16.ts ├── lesson17 └── lesson17.ts ├── lesson18 └── lesson18.ts ├── lesson19 └── lesson19.ts ├── lesson3 ├── lesson3.html ├── lesson3.js └── lesson3.ts ├── lesson4 ├── lesson4.ts └── lesson4JS.js ├── lesson5 ├── lesson5.js └── lesson5.ts ├── lesson6 ├── Lesson6.js └── Lesson6.ts ├── lesson7 ├── lesson7.js └── lesson7.ts ├── lesson8 ├── lesson8.js └── lesson8.ts └── lesson9 ├── lesson9.js └── lesson9.ts /README.md: -------------------------------------------------------------------------------- 1 | ### Typescript basic (Youtube Channel: Hỏi Dân IT) 2 | 3 | 4 | #### Full playlist here: https://www.youtube.com/playlist?list=PLncHg6Kn2JT5emvXmG6kgeGkrQjRqxsb4 -------------------------------------------------------------------------------- /chapter3/backup_chapter3.ts: -------------------------------------------------------------------------------- 1 | //lesson 41 2 | 3 | interface IEmployee { 4 | empCode: number; 5 | name: string; 6 | getSalary: (empCode1: number) => number; 7 | } 8 | 9 | class Eric implements IEmployee { 10 | public empCode: number; 11 | public name: string; 12 | 13 | getSalary = (code: number): number => { 14 | return 100 * code; 15 | } 16 | 17 | getName = () => { 18 | return 'hoi dan it' 19 | } 20 | } 21 | 22 | //lesson 40 23 | interface IPerson { 24 | readonly firstName: string; 25 | lastName?: string; 26 | address: string 27 | } 28 | 29 | type Person41 = { 30 | firstName: string; 31 | lastName: string 32 | } 33 | 34 | function getFullName(person: IPerson) { 35 | return `${person.firstName} ${person.lastName}`; 36 | } 37 | 38 | let person40 = { 39 | firstName: 'Eric', 40 | lastName: 'Hoi Dan IT', 41 | address: 'Ha noi' 42 | }; 43 | 44 | console.log(getFullName(person40)); // Eric Hoi Dan IT 45 | 46 | 47 | //lesson 38 48 | class Circle { 49 | static pi: number = 3.14; 50 | public test: number = 69; 51 | 52 | static calculateArea(radius: number) { 53 | return this.pi * radius * radius; 54 | } 55 | } 56 | 57 | let t = new Circle(); 58 | console.log(">>> check pi= ", Circle.calculateArea(10)); 59 | 60 | 61 | //lesson 37 62 | 63 | class Person3 { 64 | firstName: string; 65 | lastName: string; 66 | 67 | constructor(firstName: string, lastName: string) { 68 | this.firstName = firstName; 69 | this.lastName = lastName; 70 | } 71 | getFullName(): string { 72 | return `${this.firstName} ${this.lastName}`; 73 | } 74 | describe(): string { 75 | return `This is ${this.firstName} ${this.lastName}.`; 76 | } 77 | } 78 | 79 | //để kế thừa 1 class, chúng ta sử dụng keyword extends 80 | 81 | class Employee1 extends Person3 { 82 | private jobTitle; 83 | constructor( 84 | firstName: string, 85 | lastName: string, 86 | jobTitle: string) { 87 | 88 | // call the constructor of the Person class: 89 | super(firstName, lastName); 90 | this.jobTitle = jobTitle; 91 | } 92 | //overwrite 93 | describe(): string { 94 | return `${super.describe()} from parent - describe Hoi Dan IT`; 95 | } 96 | } 97 | 98 | // let employee = new Employee('John','Doe','Front-end Developer'); 99 | 100 | //Employee kết thừa lại person => dùng đc method của parent 101 | let employee = new Employee1('Hoi Dan IT', 'Eric', 'Web Developer'); 102 | 103 | console.log(employee.getFullName()); 104 | console.log(employee.describe()); 105 | 106 | //lesson 36 107 | class Person2 { 108 | private _age: number; 109 | public firstName: string; 110 | public lastName: string; 111 | 112 | constructor(_age: number, firstName: string, lastName: string) { 113 | this._age = _age; 114 | this.firstName = firstName; 115 | this.lastName = lastName; 116 | } 117 | 118 | //getter 119 | get age() { 120 | return this._age; 121 | } 122 | 123 | //setter 124 | set age(age2: number) { 125 | if (age2 < 0 || age2 > 150) { 126 | throw Error("Invalid age") 127 | } 128 | this._age = age2; 129 | } 130 | } 131 | 132 | let person2 = new Person2(25, 'Hoi Dan IT', 'Eric'); 133 | let checkAge = person2.age; //getter 134 | 135 | person2.age = 69 // setter 136 | // person2.setAge(69) //setter 137 | console.log(">>> check age: ", person2) //getter 138 | // person2.age = 26; 139 | 140 | //lesson 35 141 | class Person1 { 142 | readonly birthDate: Date; 143 | 144 | constructor(birthDate: Date) { 145 | this.birthDate = birthDate; 146 | } 147 | } 148 | 149 | let person = new Person1(new Date(1990, 12, 25)); 150 | // person.birthDate = new Date(1991, 12, 25); // Compile error 151 | 152 | //lesson 34 153 | class Employee { 154 | public empName: string; 155 | protected empCode: number; 156 | 157 | constructor(name: string, code: number) { 158 | this.empName = name; 159 | this.empCode = code; 160 | } 161 | } 162 | 163 | 164 | class SalesEmployee extends Employee { 165 | private department: string; 166 | 167 | constructor(name: string, code: number, department: string) { 168 | super(name, code); 169 | this.department = department; 170 | } 171 | } 172 | 173 | let emp = new SalesEmployee("John Smith", 123, "Sales"); 174 | // emp.empCode; //Compiler Error 175 | 176 | //lesson33 177 | class Person { 178 | public ssn: string; 179 | firstName: string; 180 | lastName: string; 181 | 182 | constructor(ssn: string, firstName: string, lastName: string) { 183 | this.ssn = ssn; 184 | this.firstName = firstName; 185 | this.lastName = lastName; 186 | } 187 | 188 | getFullName(): string { 189 | return `calling method: ${this.firstName} ${this.lastName}`; 190 | } 191 | } 192 | 193 | let hoidanit = new Person("123", "Hoi Dan IT", " Eric"); 194 | console.log(">> check class: ", hoidanit.getFullName()) 195 | 196 | 197 | //lesson 32 198 | 199 | function addNumbers(a: number, b: number): number { 200 | return a + b; 201 | } 202 | 203 | function addStrings(a: string, b: string): string { 204 | return a + b; 205 | } 206 | 207 | // 2 functions làm nhiệm vụ tương tự nhau => có thể gồm thành 1 với union type 208 | function add111(a: number | string, b: number | string) { 209 | if (typeof a === 'number' && typeof b === 'number') 210 | return a + b; 211 | 212 | if (typeof a === 'string' && typeof b === 'string') 213 | return a + b; 214 | } 215 | 216 | //overloading 217 | function addNew(a: number, b: number): number; 218 | 219 | function addNew(a: string, b: string): string; 220 | 221 | function addNew(a: any, b: any) { 222 | return a + b; 223 | } 224 | 225 | // console.log(">>> check add new: ", addNew(6, 9), addNew(" Hoi Dan IT", " & Eric")) 226 | 227 | 228 | class Counter { 229 | private current: number = 0; 230 | count(): number; 231 | count(target: number): number[]; 232 | count(target?: number): number | number[] { 233 | if (target) { 234 | let values: number[] = []; 235 | for (let start = this.current; start <= target; start++) { 236 | values.push(start); 237 | } 238 | return values; 239 | } 240 | return ++this.current; 241 | } 242 | } 243 | 244 | let counter111 = new Counter(); 245 | 246 | console.log(counter111.count()); // return a number 247 | console.log(counter111.count(20)); // return an array 248 | 249 | //lesson 31 250 | //spread syntax vs rest => copy all 251 | function getTotal(...numbers: number[]): number { 252 | let total = 0; 253 | numbers.forEach((num) => total += num); 254 | //[10, 20].forEach((num) => total += num); 255 | //[].forEach((num) => total += num); 256 | return total; 257 | } 258 | 259 | // console.log(getTotal()); // 0 260 | // console.log(getTotal(10, 20)); // 30 261 | // console.log(getTotal(10, 20, 30)); // 60 262 | 263 | function multiply(n: number, ...m: number[]) { 264 | let a = [69, 96, 3, 4].map((x) => { 265 | console.log("check x = ", x) 266 | return n * x 267 | }); 268 | 269 | return m.map((x) => n * x); 270 | } 271 | // 'a' gets value [10, 20, 30, 40] 272 | // const test31 = multiply(15, 69, 96, 3, 4); 273 | 274 | // console.log(">>> check test31 = ", test31) 275 | 276 | 277 | function Greet(greeting: string, ...names: string[]) { 278 | return greeting + " " + names.join(", ") + "!"; 279 | } 280 | 281 | console.log(Greet("Xin chao", "Eric &", "Hoi dan it")); // returns "Hello Steve, Bill!" 282 | 283 | //Greet("Hello");// returns "Hello !" 284 | 285 | 286 | 287 | //lesson 30 288 | 289 | let sum7 = (x: number, y: number, z: boolean = false) => { 290 | if (z === false) { 291 | return x + y; 292 | } 293 | 294 | if (z) 295 | return x - y; 296 | } 297 | 298 | console.log(">>> check sum7 = ", sum7(1, 2), sum7(1, 2, true)) 299 | 300 | //lesson 29: optional params 301 | 302 | const sum5 = (x: number, y: number, z?: number) => { 303 | if (z) return x + y + z; 304 | return x + y; 305 | } 306 | 307 | console.log(">>> check sum 4 = ", sum5(2, 5), 308 | sum5(1, 2, 3)) //NaN : not a number;; null/ undefined 309 | 310 | //lesson 28 311 | 312 | const sum3 = (x: number, y: number): number => { 313 | return x + y; 314 | } 315 | 316 | // function sum4(x: number, y: number): number { 317 | // return x + y; 318 | // } 319 | 320 | console.log(">> check sum3 = ", sum3(1, 10)) 321 | 322 | let a = 10;//number 323 | a = sum3(1, 10) //string 324 | 325 | //lesson 27 326 | // function sum(a , b) { 327 | // return a + b; 328 | // } 329 | 330 | function sum(a: number, b: number) { 331 | return a + b; 332 | } 333 | 334 | //anonymous function () => {} 335 | const sum2 = (a: number, b: number) => { 336 | return a + b; 337 | } 338 | 339 | console.log(">>> check sum: ", sum2(6, 9)) 340 | 341 | //lesson 26 342 | for (let index = 0; index < 9; index++) { 343 | 344 | // if index is odd, skip it 345 | if (index % 2 === 1) { continue; } // 1; 0 346 | 347 | // the following code will be skipped for odd numbers 348 | // console.log(index); 349 | } 350 | 351 | 352 | 353 | // let index = -1; 354 | 355 | // while (index < 9) { 356 | // index = index + 1; 357 | // if (index % 2) 358 | // continue; 359 | // console.log(index); 360 | // } 361 | 362 | 363 | let index = 9; 364 | let count = 0; 365 | 366 | do { 367 | index += 1; 368 | 369 | if (index % 2) 370 | continue; 371 | count += 1; 372 | } while (index < 99); 373 | console.log(count); // 45 374 | 375 | 376 | 377 | 378 | //lesson 25 379 | let products = [ 380 | { name: 'phone', price: 700 }, //0 381 | { name: 'tablet', price: 900 }, //1 382 | { name: 'laptop', price: 1200 } 383 | ]; 384 | 385 | let discount1 = 0; 386 | let product = products[1]; 387 | switch (product.name) { 388 | case 'phone': 389 | discount1 = 5; 390 | break; 391 | case 'tablet': 392 | discount1 = 10; 393 | // break; 394 | case 'laptop': 395 | discount1 = 15; 396 | // break; 397 | default: 398 | discount1 = 100; 399 | } 400 | console.log(`There is a ${discount1}% on ${product.name}.`); 401 | 402 | 403 | // show the products 404 | 405 | 406 | //lesson 24 407 | let counter2 = 6; 408 | do { 409 | console.log("counter = ", counter2); 410 | if (counter2 % 2 === 1) break; 411 | counter2++; 412 | } while (counter2 < 5); 413 | 414 | 415 | //lesson 23 416 | // let counter = 0; 417 | 418 | // while (counter < 5) { 419 | // console.log("counter = ", counter); 420 | // if (counter % 2 === 1) break; 421 | // counter++; 422 | // } 423 | 424 | //lesson 23 425 | let counter = 0; 426 | 427 | while (counter < 5) { 428 | console.log("counter = ", counter); 429 | if (counter % 2 === 1) break; 430 | counter++; 431 | } 432 | 433 | //lesson 22 434 | 435 | for (let i = 0; i < 10; i++) { 436 | console.log(">> i= ", i); 437 | //i = 9 => 10 438 | } 439 | 440 | let i1 = 0; 441 | for (; i1 < 10; i1++) { 442 | console.log(i1); 443 | } 444 | 445 | 446 | for (let i = 0; ; i++) { 447 | console.log(i); 448 | if (i > 9) break; // cần có if/break để không khiến vòng lặp chạy vô hạn 449 | } 450 | 451 | 452 | let i = 0; 453 | for (; ;) { 454 | console.log(i); 455 | i++; 456 | if (i > 9) break; 457 | } 458 | 459 | 460 | //lesson21 461 | const age: number = 20; 462 | 463 | switch (age) { 464 | case 20: //waterfall 465 | case 19: 466 | // code block 467 | console.log("Đã đi làm") 468 | break; 469 | case 12: 470 | console.log("Đã đi làm sinh viên") 471 | break; 472 | default: 473 | // code block 474 | console.log(">>> run default") 475 | } 476 | 477 | ////// 478 | 479 | //lesson 20 480 | let name123: string = ''; 481 | 482 | //convert to Boolean => Boolean(age) 483 | if (name123) { 484 | console.log("You can watch JAV...") 485 | } 486 | else { 487 | console.log("Oops >>> You can watch Cartoon...") 488 | } 489 | 490 | 491 | 492 | let discount: number; 493 | let itemCount = 11; 494 | 495 | if (itemCount > 0 && itemCount <= 5) { 496 | discount = 5; // 5% discount 497 | } else if (itemCount > 5 && itemCount <= 10) { 498 | discount = 10; // 10% discount 499 | } 500 | else if (itemCount > 10 && itemCount <= 15) { 501 | discount = 20; 502 | } 503 | 504 | else { 505 | discount = 15; // 15% 506 | } 507 | 508 | console.log(`You got ${discount}% discount. `); 509 | -------------------------------------------------------------------------------- /chapter3/chapter3.js: -------------------------------------------------------------------------------- 1 | let sum6 = (x, y, z = false) => { 2 | if (z === false) { 3 | return x + y; 4 | } 5 | 6 | if (z) 7 | return x - y; 8 | } 9 | 10 | console.log(">>> check sum6 = ", sum6(1, 2), sum6(1, 2, 3)) 11 | // sum6(1, 2) => z = undefined => 0 12 | // sum6(1, 2, 3) => z = 3 13 | 14 | 15 | // let sum4 = (x, y, z) => { //z: optional params 16 | // console.log(">> check z = ", z) 17 | // if (z) { 18 | // return x + y + z; 19 | // } 20 | // return x + y; 21 | 22 | // } 23 | 24 | // console.log(">>> check sum 4 = ", sum4(1, 2), sum4(1, 2, 3)) //NaN : not a number;; null/ undefined -------------------------------------------------------------------------------- /chapter3/chapter3.ts: -------------------------------------------------------------------------------- 1 | //lesson 39 2 | abstract class Employee39 { 3 | constructor(private firstName: string, private lastName: string) { 4 | } 5 | abstract getSalary(): number; //abstract method 6 | 7 | //normal method 8 | get fullName(): string { 9 | return `${this.firstName} ${this.lastName}`; 10 | } 11 | 12 | compensationStatement(): string { 13 | return `${this.fullName} makes ${this.getSalary()} a month.`; 14 | } 15 | } 16 | 17 | // const e1 = new Employee39("eric", "hoi dan it"); 18 | class FullTimeEmployee extends Employee39 { 19 | constructor(firstName: string, lastName: string, private salary: number) { 20 | super(firstName, lastName); //dùng super để kế thừa lại cha 21 | } 22 | 23 | // cần viết method này, vì nó được khai báo abstract ở trên 24 | getSalary(): number { 25 | //asdfasdfasdf 26 | //asdfasdfasfd 27 | return this.salary; 28 | } 29 | } 30 | 31 | class Contractor extends Employee39 { 32 | constructor(firstName: string, lastName: string, private rate: number, private hours: number) { 33 | super(firstName, lastName); 34 | } 35 | getSalary(): number { 36 | return this.rate * this.hours; 37 | } 38 | } 39 | 40 | const test1 = new FullTimeEmployee("eric", 'hoi dan it', 1000); 41 | console.log(">>> test 1 : ", test1.getSalary()) -------------------------------------------------------------------------------- /docs/lesson1/lesson1.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 32 | 33 | 34 | 35 |

Debugging JavaScript

36 | 37 | 38 | 39 | 40 | 41 |

42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /docs/lesson1/lesson1.js: -------------------------------------------------------------------------------- 1 | function onClick() { 2 | if (inputsAreEmpty()) { 3 | label.textContent = 'Error: one or both inputs are empty.'; 4 | return; 5 | } 6 | updateLabel(); 7 | } 8 | function inputsAreEmpty() { 9 | if (getNumber1() === '' || getNumber2() === '') { 10 | return true; 11 | } else { 12 | return false; 13 | } 14 | } 15 | function updateLabel() { 16 | var addend1 = getNumber1(); 17 | var addend2 = getNumber2(); 18 | var sum = addend1 + addend2; 19 | label.textContent = addend1 + ' + ' + addend2 + ' = ' + sum; 20 | } 21 | function getNumber1() { 22 | return inputs[0].value; 23 | } 24 | function getNumber2() { 25 | return inputs[1].value; 26 | } 27 | var inputs = document.querySelectorAll('input'); 28 | var label = document.querySelector('p'); 29 | var button = document.querySelector('button'); 30 | button.addEventListener('click', onClick); -------------------------------------------------------------------------------- /docs/lesson1/lesson1VersionTS.js: -------------------------------------------------------------------------------- 1 | function onClick() { 2 | if (inputsAreEmpty()) { 3 | label.textContent = 'Error: one or both inputs are empty.'; 4 | return; 5 | } 6 | updateLabel(); 7 | } 8 | function inputsAreEmpty() { 9 | if (!getNumber1() || !getNumber2()) { 10 | return true; 11 | } 12 | else { 13 | return false; 14 | } 15 | } 16 | function updateLabel() { 17 | var addend1 = getNumber1(); 18 | var addend2 = getNumber2(); 19 | var sum = addend1 + addend2; 20 | label.textContent = addend1 + ' + ' + addend2 + ' = ' + sum; 21 | } 22 | function getNumber1() { 23 | return Number(inputs[0].value); 24 | } 25 | function getNumber2() { 26 | return Number(inputs[1].value); 27 | } 28 | var inputs = document.querySelectorAll('input'); 29 | var label = document.querySelector('p'); 30 | var button = document.querySelector('button'); 31 | button.addEventListener('click', onClick); 32 | -------------------------------------------------------------------------------- /docs/lesson1/lesson1VersionTS.ts: -------------------------------------------------------------------------------- 1 | function onClick() { 2 | if (inputsAreEmpty()) { 3 | label.textContent = 'Error: one or both inputs are empty.'; 4 | return; 5 | } 6 | updateLabel(); 7 | } 8 | function inputsAreEmpty() { 9 | if (!getNumber1() || !getNumber2()) { 10 | return true; 11 | } else { 12 | return false; 13 | } 14 | } 15 | function updateLabel() { 16 | var addend1 = getNumber1(); 17 | var addend2 = getNumber2(); 18 | var sum = addend1 + addend2; 19 | label.textContent = addend1 + ' + ' + addend2 + ' = ' + sum; 20 | } 21 | function getNumber1(): number { 22 | return Number(inputs[0].value); 23 | } 24 | function getNumber2(): number { 25 | return Number(inputs[1].value); 26 | } 27 | var inputs = document.querySelectorAll('input'); 28 | var label = document.querySelector('p'); 29 | var button = document.querySelector('button'); 30 | button.addEventListener('click', onClick); -------------------------------------------------------------------------------- /docs/lesson2/note.txt: -------------------------------------------------------------------------------- 1 | - Cài đặt thư viện: 2 | npm install -g typescript@4.8.3 ts-node@10.9.1 -------------------------------------------------------------------------------- /docs/lesson3/lesson3.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Document 8 | 9 | 10 | 11 | Lesson 3: TypeScript Hello, World! 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /docs/lesson3/lesson3.js: -------------------------------------------------------------------------------- 1 | var message = "hello world"; 2 | console.log(message); 3 | -------------------------------------------------------------------------------- /docs/lesson3/lesson3.ts: -------------------------------------------------------------------------------- 1 | const message: string = "hello world"; 2 | console.log(message); -------------------------------------------------------------------------------- /docs/lesson4/lesson4.ts: -------------------------------------------------------------------------------- 1 | const sum3 = (x: number, y: number) => { 2 | return x + y; 3 | } 4 | 5 | //no error 6 | console.log(sum3(5, 10)); 7 | 8 | //error 9 | // console.log(sum3(5, '15')); -------------------------------------------------------------------------------- /docs/lesson4/lesson4JS.js: -------------------------------------------------------------------------------- 1 | 2 | //perfect 3 | const sum = (x, y) => { 4 | return x + y; 5 | } 6 | 7 | sum(5, 10); //15 8 | 9 | //need to validate 10 | const sum2 = (x, y) => { 11 | //validate: x, y are numbers ??? 12 | return x + y; 13 | } 14 | 15 | sum('name', 20); 16 | -------------------------------------------------------------------------------- /docs/lesson5/lesson5.js: -------------------------------------------------------------------------------- 1 | const name = 'Eric'; 2 | 3 | console.log("length = ",) 4 | console.log('upPerCasse = ',) 5 | -------------------------------------------------------------------------------- /docs/lesson5/lesson5.ts: -------------------------------------------------------------------------------- 1 | const nameTS = 'Eric'; 2 | 3 | console.log('length = ',) 4 | 5 | console.log('upPerCasse = ',) 6 | 7 | -------------------------------------------------------------------------------- /docs/lesson6/Lesson6.js: -------------------------------------------------------------------------------- 1 | let count = 1; //ok 2 | 3 | count = 'name'; //error ? 4 | 5 | console.log(">>> check name: ") 6 | 7 | let names = ['Eric', 'Hỏi Dân IT', 'Typescript'] // array chỉ bao gồm string 8 | names.push(25) //error ? 9 | -------------------------------------------------------------------------------- /docs/lesson6/Lesson6.ts: -------------------------------------------------------------------------------- 1 | let count1 = 1; //ok 2 | 3 | // count1 = 'name'; //error ? 4 | 5 | console.log(">>> check name: ") 6 | 7 | let names1 = ['Eric', 'Hỏi Dân IT', 'Typescript'] // array chỉ bao gồm string 8 | // names1.push(25) //error ? 9 | -------------------------------------------------------------------------------- /docs/lesson7/lesson7.js: -------------------------------------------------------------------------------- 1 | let count1 = 10; -------------------------------------------------------------------------------- /docs/lesson7/lesson7.ts: -------------------------------------------------------------------------------- 1 | let count = 10; -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Document 8 | 9 | 10 | 11 | hello world with live server 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /lesson.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haryphamdev/hoidanit-typescript-basic/39b4da6a058b411060df0a38dad39c9474f8d6e6/lesson.js -------------------------------------------------------------------------------- /projects/lesson10/lesson10.js: -------------------------------------------------------------------------------- 1 | let status = false; 2 | let check = true; -------------------------------------------------------------------------------- /projects/lesson10/lesson10.ts: -------------------------------------------------------------------------------- 1 | let status1: boolean = false; 2 | let check1: boolean = true; 3 | let pro: boolean = Boolean(""); 4 | 5 | console.log(">>> check pro: ", pro) 6 | 7 | -------------------------------------------------------------------------------- /projects/lesson11/lesson11.js: -------------------------------------------------------------------------------- 1 | let mine = {}; 2 | mine = { name: "Eric" }; 3 | 4 | mine.address = "Hỏi Dân IT"; 5 | 6 | console.log(">>> check mine: ", mine) -------------------------------------------------------------------------------- /projects/lesson11/lesson11.ts: -------------------------------------------------------------------------------- 1 | // let person: object; 2 | 3 | // let mine3 = {name : 'hoi dan it'} 4 | 5 | // person = { name: 'Eric'}; 6 | // person = 'Eric' 7 | 8 | // let pro = { 9 | // name: "Eric", //string 10 | // age: 25 //age 11 | // } 12 | 13 | let pro1: { 14 | name: string, 15 | age: number 16 | } = { 17 | name: "Eric", //string 18 | age: 25 //age 19 | } 20 | 21 | -------------------------------------------------------------------------------- /projects/lesson12/lesson12.js: -------------------------------------------------------------------------------- 1 | let myarr = ["eric", "hoi dan it", 25] -------------------------------------------------------------------------------- /projects/lesson12/lesson12.ts: -------------------------------------------------------------------------------- 1 | let test: (number | string)[] = ['hoi dan it', 69]; 2 | 3 | test.push('hoi dan it'); 4 | 5 | test.push(25); 6 | 7 | console.log(">>> check array: ", test) -------------------------------------------------------------------------------- /projects/lesson13/lesson13.ts: -------------------------------------------------------------------------------- 1 | let skills: (string | number)[] = ['Hỏi Dân IT', 25]; 2 | skills.push('coding'); 3 | skills.push(69); 4 | 5 | //tuple: dataType/size/order 6 | let skills2: [string, number] = ['Hỏi Dân IT', 25]; 7 | 8 | let skills3: [boolean, string, number?]; 9 | 10 | skills3 = [true, 'Eric']; 11 | 12 | 13 | -------------------------------------------------------------------------------- /projects/lesson14/lesson14.ts: -------------------------------------------------------------------------------- 1 | enum API_STATUS { 2 | PENDDING, 3 | FULFILLED = "FULFILLED asfdasfasf", 4 | REJECTED = "FULFILLED" //PROMISE 5 | } 6 | 7 | //constant 8 | //java 9 | 10 | let a1 = API_STATUS.PENDDING; 11 | let a2 = API_STATUS.FULFILLED; 12 | 13 | //frontend : syntax 14 | console.log(">>> a1 = ", a1, " a2 = ", a2) -------------------------------------------------------------------------------- /projects/lesson15/lesson15.js: -------------------------------------------------------------------------------- 1 | let name = 'eric'; //string 2 | 3 | name = 35; //number 4 | 5 | name = false; //boolean -------------------------------------------------------------------------------- /projects/lesson15/lesson15.ts: -------------------------------------------------------------------------------- 1 | let namev2: any = 'Eric'; // any everywhere 2 | 3 | namev2 = true; 4 | 5 | -------------------------------------------------------------------------------- /projects/lesson16/lesson16.ts: -------------------------------------------------------------------------------- 1 | const sum = (a: number, b: number): number => { 2 | return a + b; 3 | } 4 | 5 | const handleLogs = (message: string): void => { 6 | console.log(">>> message: ", message) 7 | 8 | } -------------------------------------------------------------------------------- /projects/lesson17/lesson17.ts: -------------------------------------------------------------------------------- 1 | function handleException(errorMessage: string): never { 2 | throw Error(errorMessage) 3 | } 4 | 5 | function runInfinity(): void { 6 | // while (true) { 7 | // // console.log("running...") 8 | // } 9 | } 10 | 11 | // handleException("just a test error"); 12 | 13 | let a = handleException("just a test error"); 14 | console.log(">> check a= ", a) -------------------------------------------------------------------------------- /projects/lesson18/lesson18.ts: -------------------------------------------------------------------------------- 1 | function addNumberOrString(a: number | string | object | boolean, b: number | string) { 2 | if (typeof a === 'number' && typeof b === 'number') { 3 | return a + b; 4 | } 5 | if (typeof a === 'string' && typeof b === 'string') { 6 | return a.concat(b); 7 | } 8 | throw new Error('Parameters must be numbers or strings'); 9 | } 10 | 11 | //loi khi running 12 | //loi khi compile 13 | console.log(">>> check: ", addNumberOrString(true, "Hoi dan it")) 14 | 15 | -------------------------------------------------------------------------------- /projects/lesson19/lesson19.ts: -------------------------------------------------------------------------------- 1 | type ericType = number | string | object | boolean; 2 | function addNumberOrString(a: ericType, b: number | string) { 3 | if (typeof a === 'number' && typeof b === 'number') { 4 | return a + b; 5 | } 6 | if (typeof a === 'string' && typeof b === 'string') { 7 | return a.concat(b); 8 | } 9 | throw new Error('Parameters must be numbers or strings'); 10 | } 11 | 12 | //loi khi running 13 | //loi khi compile 14 | console.log(">>> check: ", addNumberOrString('eric ', "Hoi dan it")) -------------------------------------------------------------------------------- /projects/lesson3/lesson3.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Document 8 | 9 | 10 | 11 | Lesson 3: TypeScript Hello, World! update 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /projects/lesson3/lesson3.js: -------------------------------------------------------------------------------- 1 | var message = "hello world"; 2 | console.log(message); 3 | -------------------------------------------------------------------------------- /projects/lesson3/lesson3.ts: -------------------------------------------------------------------------------- 1 | const message: string = "hello world"; 2 | console.log(message); -------------------------------------------------------------------------------- /projects/lesson4/lesson4.ts: -------------------------------------------------------------------------------- 1 | const sum3 = (x: number, y: number) => { 2 | return x + y; 3 | } 4 | 5 | //no error 6 | // console.log(">>> check sum3 = ", sum3(5, 10)); //15 7 | 8 | 9 | //error 10 | console.log(">>> check sum3 = ", sum3(5, '10')); //15 11 | -------------------------------------------------------------------------------- /projects/lesson4/lesson4JS.js: -------------------------------------------------------------------------------- 1 | 2 | //perfect 3 | const sum = (x, y) => { 4 | return x + y; 5 | } 6 | 7 | console.log(">>> check sum1 = ", sum(5, 10)); //15 8 | 9 | //need to validate 10 | const sum2 = (x, y) => { 11 | //validate: x, y are numbers ??? 12 | return x + y; 13 | } 14 | 15 | console.log(">>> check sum2 = ", sum2('name', 10)); //15 16 | 17 | -------------------------------------------------------------------------------- /projects/lesson5/lesson5.js: -------------------------------------------------------------------------------- 1 | const name = 'Eric'; 2 | 3 | console.log("length = ", name.length) 4 | console.log('upPerCasse = ', name.toUpperCase()) 5 | -------------------------------------------------------------------------------- /projects/lesson5/lesson5.ts: -------------------------------------------------------------------------------- 1 | const nameTS1 = 'Eric'; 2 | 3 | console.log('length = ', nameTS1.length) 4 | 5 | console.log('upPerCasse = ', nameTS1.toUpperCase()) 6 | 7 | -------------------------------------------------------------------------------- /projects/lesson6/Lesson6.js: -------------------------------------------------------------------------------- 1 | // let count = 1; //ok 2 | 3 | // count = 'name'; //error ? 4 | 5 | // console.log(">>> check count: ", count) 6 | 7 | let names = ['Eric', 'Hỏi Dân IT', 'Typescript'] // array chỉ bao gồm string 8 | names.push(25) //error ? 9 | 10 | console.log(">>> check names: ", names) 11 | 12 | 13 | -------------------------------------------------------------------------------- /projects/lesson6/Lesson6.ts: -------------------------------------------------------------------------------- 1 | 2 | 3 | let names2: string[] = ['Eric', 'Hỏi Dân IT', 'Typescript'] 4 | // array chỉ bao gồm string 5 | names2.push("25") //error ? 6 | 7 | console.log(">>> check name 2: ", names2) 8 | -------------------------------------------------------------------------------- /projects/lesson7/lesson7.js: -------------------------------------------------------------------------------- 1 | let count0 = 10; 2 | 3 | count0 = 'eric'; 4 | 5 | console.log(">>> check count 0 = ", count0) 6 | -------------------------------------------------------------------------------- /projects/lesson7/lesson7.ts: -------------------------------------------------------------------------------- 1 | let count: string = "Hoi Dan IT"; 2 | 3 | console.log(">>> check count 0 = ", count) 4 | 5 | let test = ['eric', 'hoi danit', 'typescirpt', 96]; 6 | 7 | test.push(25); -------------------------------------------------------------------------------- /projects/lesson8/lesson8.js: -------------------------------------------------------------------------------- 1 | let count = 69; //int long => number 2 | let score = 9.6; //float double => number 3 | -------------------------------------------------------------------------------- /projects/lesson8/lesson8.ts: -------------------------------------------------------------------------------- 1 | let test: number = 9.6; 2 | 3 | const a1: number = 123; 4 | 5 | let a2: number; 6 | 7 | test = 999999999999999999999999999999999999; 8 | 9 | 10 | -------------------------------------------------------------------------------- /projects/lesson9/lesson9.js: -------------------------------------------------------------------------------- 1 | let name = "Eric"; 2 | let name1 = 'Eric'; 3 | -------------------------------------------------------------------------------- /projects/lesson9/lesson9.ts: -------------------------------------------------------------------------------- 1 | let name2: string = ` và "Hỏi Dân IT"`; 2 | let name3 = `Eric ${name2}`; //template strings js 3 | 4 | console.log(">>> check name = ", name3) 5 | // Eric và "Hỏi Dân IT" --------------------------------------------------------------------------------