josdejong / mathjs

An extensive math library for JavaScript and Node.js
https://mathjs.org
Apache License 2.0
14.31k stars 1.24k forks source link

types: quantileSeq expands types unnecessarily #3197

Closed domdomegg closed 3 months ago

domdomegg commented 4 months ago

Describe the bug

The types for quantileSeq have an overly wide return type.

To Reproduce

// Type 'number | BigNumber | Unit | MathArray' is not assignable to type 'number'.
//   Type 'BigNumber' is not assignable to type 'number'.ts(2322)
const value: number = math.quantileSeq([1, 2, 3], 0.90);

I think value here should always be a number. This is the behaviour of for example math.mean and math.median.

josdejong commented 4 months ago

Thanks for your input, we could indeed improve the TypeScript definitions I think. Help would be welcome.

josdejong commented 3 months ago

Fixed now in v12.4.3

Loksly commented 2 months ago

Hi. This fix https://github.com/josdejong/mathjs/pull/3198/files broke basic usage:

Now this basic usage is broken.

const quantiles: number[] = math.quantileSeq([1, 2, 3, 4, 5], [0.25, 0.75]) as number[];

This was supported previous to v12.4.3.

domdomegg commented 2 months ago

Thanks @Loksly. I've raised a PR to fix this: https://github.com/josdejong/mathjs/pull/3223

In the mean time this workaround should work:

const quantiles: number[] = math.quantileSeq([1, 2, 3, 4, 5], [0.25, 0.75]) as unknown as number[];
josdejong commented 2 months ago

This fix is now published in v13.0.2