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

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

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 Relatório

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