Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

340
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!