Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

118
Vistas
I'm trying to add an element to an object I previously created but I get an error that property does not exist on type {} javascript

I just started learning javascript and I'm trying to add an element to an object I previously created but I get an error that property does not exist.

I cant seem to find the issue, maybe its a syntax error but i cant seem to see it so maybe someone can help me spot it if thats the case?

This is the object >

const restaurant = {
  name: 'Classico Italiano',
  location: 'Via Angelo Tavanti 23, Firenze, Italy',
  categories: ['Italian', 'Pizzeria', 'Vegetarian', 'Organic'],
  starterMenu: ['Focaccia', 'Bruschetta', 'Garlic Bread', 'Caprese Salad'],
  mainMenu: ['Pizza', 'Pasta', 'Risotto'],

  openingHours: {
    thu: {
      open: 12,
      close: 22,
    },
    fri: {
      open: 11,
      close: 23,
    },
    sat: {
      open: 0, // Open 24 hours
      close: 24,
    },
  },
  orderD: function ({
    orderName = 'ANON',
    address = 'ANON',
    time = '00:00',
    order = 'ANON',
  }) {
    console.log(`${orderName} has ordered (${order}) to ${address} at ${time}`);
  },
  orderPizza: function (mainIngredient, ...otheringredients) {
    console.log(mainIngredient);
    console.log(otheringredients);
  },
};

Then here I tried to add an element to it.

restaurant.numOfGuests = 0;

but then it highlights the element name saying

Property 'numOfGuests' does not exist on type '{ name: string; location: string; categories: string[]; starterMenu: string[]; mainMenu: string[]; openingHours: { thu: { open: number; close: number; }; fri: { open: number; close: number; }; sat: { open: number; close: number; }; }; orderD: ({ orderName, address, time, order, }: { ...; }) => void; orderPizza: (mai...'.ts(2339)

I tried to ignore it and move on but then when i tried logging the new element to the console

console.log(numOfGuests);

It gave me this error

Uncaught ReferenceError: numOfGuests is not defined

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

It doesn't appear that you have set the numOfGuests property, which is important if your code is TypeScript (as opposed to JavaScript). All you have to do is set it as in the following code. Then, you can reference it in any other code as needed. See Object Types for more information.

const restaurant = {
  name: 'Classico Italiano',
  location: 'Via Angelo Tavanti 23, Firenze, Italy',
  categories: ['Italian', 'Pizzeria', 'Vegetarian', 'Organic'],
  starterMenu: ['Focaccia', 'Bruschetta', 'Garlic Bread', 'Caprese Salad'],
  mainMenu: ['Pizza', 'Pasta', 'Risotto'],
  numOfGuests: 20,

  openingHours: {
    thu: {
      open: 12,
      close: 22,
    },
    fri: {
      open: 11,
      close: 23,
    },
    sat: {
      open: 0, // Open 24 hours
      close: 24,
    },
  },
  orderD: function({
    orderName = 'ANON',
    address = 'ANON',
    time = '00:00',
    order = 'ANON',
  }) {
    console.log(`${orderName} has ordered (${order}) to ${address} at ${time}`);
  },
  orderPizza: function(mainIngredient, ...otheringredients) {
    console.log(mainIngredient);
    console.log(otheringredients);
  },
};
console.log(restaurant);

If you're using JavaScript, you shouldn't get the error. See the following code snippet as an example:

const restaurant = {
  name: "Classico Italiano",
  location: "Via Angelo Tavanti 23, Firenze, Italy",
  categories: ["Italian", "Pizzeria", "Vegetarian", "Organic"],
  starterMenu: ["Focaccia", "Bruschetta", "Garlic Bread", "Caprese Salad"],
  mainMenu: ["Pizza", "Pasta", "Risotto"],

  openingHours: {
    thu: {
      open: 12,
      close: 22
    },
    fri: {
      open: 11,
      close: 23
    },
    sat: {
      open: 0, // Open 24 hours
      close: 24
    }
  },
  orderD: function({
    orderName = "ANON",
    address = "ANON",
    time = "00:00",
    order = "ANON"
  }) {
    console.log(`${orderName} has ordered (${order}) to ${address} at ${time}`);
  },
  orderPizza: function(mainIngredient, ...otheringredients) {
    console.log(mainIngredient);
    console.log(otheringredients);
  }
};
restaurant.numOfGuests = 20;
console.log(restaurant);

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda