Chunshen Li

Chunshen Li

  • NA
  • 309
  • 17k

Ok, I'm less than an hour into learning TypeScript

Oct 15 2020 6:23 PM
The following code works, however, I feel it's less than elegant. I added the drink() function.
 
How to improve it? Thanks.
  1. interface Person {  
  2.     age: number,  
  3.     name: string,  
  4.     say(): string  
  5.   //  drink(): string  
  6. }  
  7.   
  8. let mike = {  
  9.     age: 15,   
  10.     name:"Mike",   
  11.     drink: function() {   
  12.           if (this.age < 18) {  
  13.             return ("Sorry, you're too young to drink.");  
  14.           }  
  15.         else {  
  16.             return ("You are old enough to drink.");  
  17.             }    
  18.      }  
  19.      say: function() {         
  20.             return "My name is " + this.name +   
  21.                " and I'm " + this.age + " years old!"   
  22.     }  
  23. }  
  24.   
  25. function sayIt(person: Person) {  
  26.     return person.say();  
  27.     //return person.drink();  
  28. }  
  29.   
  30. function drinkIt(person: Person) {  
  31.     return person.drink();  
  32. }  
  33.   
  34. console.log(sayIt(mike),drinkIt(mike));

Answers (1)