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

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

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 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