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

341
Vistas
How to subtract two numbers in TypeScript?

I am trying to subtract two numbers but I get The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.:

const ls = Object.values(localScore)[0];
const s = Object.values(score)[0];
console.log(typeof ls); // number
console.log(typeof s); // number
const diff = s - ls;
              // ^ The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.

What I am doing wrong? I am using TypeScript.

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

0

While object keys are deterministically-ordered in some runtime environments, it's still not safe to perform operations on values held in two separate objects, accessing them by a correlated numeric index.

According to the information in your comment, it sounds like you don't know the name of the property holding the value that you want to target in each object, but you do know that the name is the same in both objects, so a safer approach would be:

TS Playground

/** Given that values in the objects are correlated by property key: */
function getDiff <
  K extends PropertyKey,
  T extends Record<K, number>,
>(minuend: T, subtrahend: T, key: K): number {
  return minuend[key] - subtrahend[key];
}

type NumberValues = Record<string, number>;
declare const localScore: NumberValues;
declare const score: NumberValues;

const [key] = Object.keys(localScore); // destructure first enumerable property key
const diff = getDiff(score, localScore, key); // number, but possibly negative
const absoluteDiff = Math.abs(diff);
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