TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Chunshen Li
NA
309
17.8k
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.
interface
Person {
age: number,
name: string,
say(): string
// drink(): 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();
//return person.drink();
}
function
drinkIt(person: Person) {
return
person.drink();
}
console.log(sayIt(mike),drinkIt(mike));
Reply
Answers (
1
)
Remove object from JSON which is matched into Netsted Object
error on event.total object possibly is undefined when Upload file?