getter method returns an object instead of a string

Viewed 23

I am learning typescript and want to do simple class that implements an interface:

interface IBook {
  title: string;
  description: string;
  ...
}

class Book implements IBook {
  private _title: string = "";
  private _description: string = "";

  constructor(title: string, description: string) {
    this._title = title;
    this._description = description;
  }

  get title() {
    return this._title;
  }

  get description() {
    return this._description;
  }
}

now when I create a new note and try to get its title, I instead get {title:"", description:""} and if i do note.description, i get undefined:

const book = new Book("BOok title", "book note");
console.log(book) // "title": {title:"BOok title", description: "book note"}
console.log(book.title) // {title:"BOok title", description: "book note"}
console.log(book.description); //undefined

what am I doing wrong that book.title just doesn't return title of the book?

0 Answers
Related