#Interface type checking error

12 messages · Page 1 of 1 (latest)

patent arrow
#

Your problem is that the Menu interface says setMeal should be a function, but in your menu it's implemented as a setter, not a function, because you have set setMeal

#

Remove the set should do it.

#

You can simply do setMeal: string, getter + setter is just a regular property.

#

Although if you aren't going to have a getter and only a setter, then better to leave it as a function.

#

No, setMeal: string would mean you need to implement it as set setMeal(value: string)

#

setMeal: number would be set setMeal(value: number), etc.

patent arrow
#

You mean a property with only setter but without getter?

#

I don't think TS supports that and it's not really a good idea either, if you have only a setter but no getter you should just keep it as a function.

#

That's not how you do getters and setters, they are supposed to be a pair.

#
interface Menu {
  _meal: string;
  _price: number;
  meal: string;
}

const menu: Menu = {
  _meal: "",
  _price: 0,
  set meal(value: string) {
    this._meal = value;
  },
  get meal() {
    return this._meal;
  }
};
#

set meal implements what happens when you do menu.meal = 'burrito' while get meal implements what happens when you do console.log(menu.meal).

#

For more information you can read get and set.