我有一个计算多边形面积的函数,这是我的函数
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;
}
当我将函数复制到打字稿类并使用它时,我收到了这个错误
运算符 '+' 不能应用于类型 'number | math.BigNumber' 和 '数字 | 数学.BigNumber'
我打字稿中的功能是
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;
}
如何解决这个问题,以便我可以在我的打字稿类上使用我的函数?