josdejong / josdejong/mathjs

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

Open
#3,660 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
15.1k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

Description

**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(x: T, y: MathType): T
dotMultiply(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(x: T, y: MathType): Matrix
multiply(x: MathType, y: T): Matrix

multiply(x: T, y: T[]): T
multiply(x: T[], y: T): T
multiply(x: T[], y: T[]): T[]
multiply(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(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` 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](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgYygUwIYzQGjhgGwLgF84AzKCEOAIhCwAsArAZ1oG4AoL5CAO1bwGMRnAC8KdFjQAKQgTwISASm68BQuAA8JcEYwB0AV37AYs2gEYADHABytNRsHwAnqz0BtG4at4AWl8AZkCAFgBddT5XfWMCGGAwAjcAJTRWePhJA0MQLKSU2W08D2cYrQATCBgAWQLktIysvVzquoaikrgy9QB6PrhU00SQNDgIYxgwKbhgTz4odGQYAC4XVggCNEMCCABzWXyEwqbMhPLNLZ29w-b6k8b085hnLgG4AK+4ABU3MHGwEEAJWwAEcEYaAIAKgni+AR4MH+4wAotoQTAADw-OBobTYfiVTwwKDGNAAPj0P3USIBcAAkqwUQBHYyETEAQTwACFKeIuHA4LJMQBxcmyFQSSki3H4tCEzwcuAAfjgVjgqzgACZJXiCUSBULReLJeJpbL9Z5uSq1RrtSpDYLVSSyY67eRCKw0P1BgB1RhYX4AZTm-HIaCWaEq61p4weiUa9LDEfQlT0sYg5DijxSzyy3FjcAAIjV46ck+HI2nJBms-dOmd8zwPv7A160CBPHiMVH8PBSfxRmgY8i4Gie9W4LWdF4os3Bj9IV64KxGJMCGnMshkGhe8As0DK-Kd-osMgl3AB0OVwGAYZ3oMAMLGSODlJTyFuCgYYDEQaHiMpwgCA4AAIyoDBKhSe9CzLRogzXeJKm5NAAFUzBgDklgwL9+UFcc0BWTFGRZNkCExOCUgrFMozwAiVijclyRpUcSw6HM3AQ9dkLQjCsKgHCJENeisRI1l2TYyi3Goqs6PRQjsEqJiOCAA)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.