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

151
Views
cannot use function from js in typescript

i have a function to calculate area of polygon, and here's my function

import {distance} from "mathjs"
function getArea(arrayCord) {
    let triangle =[]
    let area = 0.0
    let a =0.0
    let b =0.0
    let c =0.0
    let s =0.0
    for (let i = 0; i < arrayCord.length-2; i++) {
        a = distance(arrayCord[i],arrayCord[i+1])// p1-p2
        b = distance(arrayCord[i+1],arrayCord[i+2]) //p2 - p3
        c = distance(arrayCord[i],arrayCord[i+2]) //p3-p1
        s = (a+b+c)/2;
        triangle[i] = Math.sqrt(s*(s-a)*(s-b)*(s-c))
        area+= triangle[i]
    }
    return area;
}

when i copy the function into a typescript class and use it, i got this error that said

Operator '+' cannot be applied to types 'number | math.BigNumber' and 'number | math.BigNumber'

enter image description here

the function in my typescript was


          const getArea = (arryy:(number|undefined)[][]) =>{
            let triangle =[]
            let area = 0.0
            let a:(number|BigNumber) =0.0
            let b:(number|BigNumber) =0.0
            let c:(number|BigNumber) =0.0
            let s:(number|BigNumber) =0.0
            for (let i = 0; i < arryy.length-2; i++) {
              a = distance(arryy[i],arryy[i+1])// p1-p2
              b = distance(arryy[i+1],arryy[i+2]) //p2 - p3
              c = distance(arryy[i],arryy[i+2]) //p3-p1
              s = (a+b+c)/2;
              triangle[i] = Math.sqrt(s*(s-a)*(s-b)*(s-c))
              area+= triangle[i]
            }
            return area;
        }

how to solve this problem so that i can use my function on my typescript class?

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You cannot use regular arithmetic on BigNumbers.

Replace

s = (a+b+c)/2

with

s = math.divide(math.add(a, b, c), 2)

But you need to beware that math.divide returns number | BigNumber | Fraction | Complex | Unit | Array | Matrix so your s may need to have its type adjusted.

See:

  • https://mathjs.org/docs/reference/functions/divide.html
  • https://mathjs.org/docs/reference/functions/add.html
about 4 years ago · Santiago Trujillo Report

0

number and BigNumber are seen as two data types, and Typescript doesn't know this addition operation is permitted. So in your case, a could be a number and b could be a BigNumber.

If you know for sure these can be added together, you can use // @ts-ignore I'm sure this adds on the line above the comment where error is shown.

about 4 years ago · Santiago Trujillo 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!