Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Further improve quantileSeq typings #3223

Merged
merged 2 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions test/typescript-tests/testTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2543,6 +2543,12 @@ Statistics functions' return types
>()

expectTypeOf(math.quantileSeq([1, 2, 3], 0.75)).toMatchTypeOf<number>()
expectTypeOf(math.quantileSeq([1, 2, 3, 4, 5], [0.25, 0.75])).toMatchTypeOf<
MathArray | MathScalarType
>()
expectTypeOf(
math.quantileSeq([1, 2, 3, 4, 5], [0.25, 0.75]) as number[]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI this doesn't test anything because you check for a type that you previously asserted. :)
I may be available to prepare a PR for the quantileSeq function this weekend if you are interested!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tafaust This tests that it can be cast without throwing an error. See discussion in #3197 (comment)

See L2546 for the equivalent test checking the output type matches the more generic correct type.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, so what this PR does is making

math.quantileSeq([1, 2, 3, 4, 5], [0.25, 0.75]) as number[]

valid TS, rather than requiring:

math.quantileSeq([1, 2, 3, 4, 5], [0.25, 0.75]) as unknown as number[]

It would be nice of course to improve the types further, so that the casting to as number[] is not needed at all. A PR would be welcome :)

).toMatchTypeOf<number[]>()
expectTypeOf(math.quantileSeq([[1, 2, 3]], 0.75)).toMatchTypeOf<number>()
expectTypeOf(
math.quantileSeq([math.bignumber('123')], 0.75)
Expand Down
2 changes: 1 addition & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2866,7 +2866,7 @@ export interface MathJsInstance extends MathJsFactory {
*/
quantileSeq<T extends MathScalarType>(
A: T[] | T[][],
prob: number | BigNumber | MathArray,
prob: number | BigNumber,
sorted?: boolean
): T
/**
Expand Down