The following code works, however, I feel it's less than elegant. I added the drink() function.
How to improve it? Thanks.
- interface Person {
- age: number,
- name: string,
- say(): string
-
- }
-
- let mike = {
- age: 15,
- name:"Mike",
- drink: function() {
- if (this.age < 18) {
- return ("Sorry, you're too young to drink.");
- }
- else {
- return ("You are old enough to drink.");
- }
- }
- say: function() {
- return "My name is " + this.name +
- " and I'm " + this.age + " years old!"
- }
- }
-
- function sayIt(person: Person) {
- return person.say();
-
- }
-
- function drinkIt(person: Person) {
- return person.drink();
- }
-
- console.log(sayIt(mike),drinkIt(mike));