#3660·mathjs

dotMultiply/multiply with Unit and array input returns the right runtime value, but the TypeScript overload is not right

Author: AncientSwordRageCreated May 23, 2026Updated May 23, 2026

Describe the bug The code math.multiply(math.unit('10 N'), [0.1, -0.3, -4]) and math.dotMultiply(math.unit('10 N'), [0.1, -0.3, -4]) give the correct result ([1N, -3N, -40N]) as far as I can tell, but the type definition cannot decide what to call this (I'm not even sure myself).

  dotMultiply<T extends MathCollection>(x: T, y: MathType): T
  dotMultiply<T extends MathCollection>(x: MathType, y: T): T
  dotMultiply(x: Unit, y: MathType): Unit
  dotMultiply(x: MathType, y: Unit): Unit
  dotMultiply(x: MathNumericType, y: MathNumericType): MathNumericType

Either of dotMultiply(x: Unit, y: MathType): Unit or it's conjugate suggest a single value Unit (which CMIIR, [1N, -3N, -40N], cannot be.)

Using regular multiply also doesn't help

  multiply<T extends Matrix>(x: T, y: MathType): Matrix
  multiply<T extends Matrix>(x: MathType, y: T): Matrix

  multiply<T extends MathArray>(x: T, y: T[]): T
  multiply<T extends MathArray>(x: T[], y: T): T
  multiply<T extends MathArray>(x: T[], y: T[]): T[]
  multiply<T extends MathArray>(x: T, y: T): MathScalarType
  multiply(x: Unit, y: Unit): Unit
  multiply(x: number, y: number): number
  multiply(x: MathType, y: MathType, ...values: MathType[]): MathType
  multiply<T extends MathType>(x: T, y: T, ...values: T[]): T

As typescript wants to pick multiply(x: MathType, y: MathType, ...values: MathType[]): MathType

But although MathType includes a MathCollection which ultimately can be narrowed down to Unit[], adding two of these together doesn't seem to work as I'd hoped.

totalGravity is a MathArray<Unit> because of zeroGravityVector Image

gravity is of type Unit and directionAB is a [number, number, number] so the result should be Unit[] Image

But they can't be added. Image

I've lost track of all the different way I've tried to cast them together.

To Reproduce Inspect math.multiply(math.unit('10 N'), [0.1, -0.3, -4]) or math.dotMultiply(math.unit('10 N'), [0.1, -0.3, -4 ]) in a typescript environment, such as this ts playground link