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

123
Vistas
Missing properties for type (typescript)

In the following:

@Mutation
remove_bought_products(productsToBeRemoved: Array<I.Product>) {

    const tmpProductsInVendingMachine: Array<I.Product> = Object.values(this.productsInVendingMachine);
    const reducedProductsInVendingMachine: Array<I.Product> =
        tmpProductsInVendingMachine.reduce((tmpProductsInVendingMachine, { id, ...rest }) => ({ ...tmpProductsInVendingMachine, ...{ [id]: { id, ...rest } } }), {});
    productsToBeRemoved.forEach(({ id }) => reducedProductsInVendingMachine[id].productQty--);
...

gives:

TS2740: Type '{}' is missing the following properties from type 'Product[]': length, pop, 
push, concat, and 28 more.
250 |
251 |         const tmpProductsInVendingMachine: Array<I.Product> = Object.values(this.productsInVendingMachine);
> 252 |         const reducedProductsInVendingMachine: Array<I.Product> =
    |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
253 |             tmpProductsInVendingMachine.reduce((tmpProductsInVendingMachine, { id, ...rest }) => ({ ...tmpProductsInVendingMachine, ...{ [id]: { id, ...rest } } }), {});
254 |         productsToBeRemoved.forEach(({ id }) => reducedProductsInVendingMachine[id].productQty--);

What type does the reducer return?

Products are objects that need to be indexed over their id; e.g.

[{
    id: 1,
    productName: "coke",
    productPrice: 2, 
    productQty: 20
},
...
]
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

reducedProductsInVendingMachine is not an Array, its an Object.

One possibility is to cast {} to the correct type in the initialization parameter of Array.prototype.reduce():

const reducedProductsInVendingMachine =
    tmpProductsInVendingMachine.reduce(
        (tmpProductsInVendingMachine, { id, ...rest }) =>
            ({ ...tmpProductsInVendingMachine, ...{ [id]: { id, ...rest } } }),
        {} as { [key: I.Product['id']]: I.Product }
    );

Notice how the implementation compiles and the reducedProductsInVendingMachine variable type is correctly inferred to { [key: I.Product['id']]: I.Product } (with I.Product['id'] resolved to whatever it is)

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