Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

123
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda