question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Type Math.min & Math.max using generic

See original GitHub issue

Search Terms

  • Math.min generic
  • Math.max generic

Suggestion

Currently, Math.min is typed as:

min(...values: number[]): number;

However, I would like to change the definition to:

min<T extends number>(...values: [T, ...T[]]): T;
min(): number; // Because this will return Infinity

Because Math.min should never return things that aren’t its input. (Except when non-number is passed in, then it’ll return NaN. But that’s impossible with this typing.)

(Okay, there’s another case: if no value is passed in, it’ll return Infinity. Unfortunately, there’s no literal type for Infinity so we can’t type that correctly. ~And AFAIK there’s no way to force at least 1 parameter with rest args.~ Updated: there is, using tuple type: [T, ...T[]]. Proposal updated. Still, it would be nice to have literal Infinity type.)

(The same applies with Math.max, except Infinity is now -Infinity)

Use Cases

Let’s say I have this type:

type TBankNoteValues = 1 | 5 | 10 | 20 | 50 | 100;

And I want to know what is the highest-value banknote I have in the array. I could use Math.max to find that out. But with the current typing, the return value isn’t guaranteed to be TBankNoteValues.

let values: TBankNoteValues[] = [50, 100, 20];
let maxValues = Math.max(...values); // number

And now I can’t pass maxValues to a function that expects TBankNoteValues anymore.

Examples

type TBankNoteValues = 1 | 5 | 10 | 20 | 50 | 100;

let values: TBankNoteValues[] = [50, 100, 20];
let maxValues = Math.max(...values);
// Current type: number
// Expected type: TBankNoteValues

Playground link

Checklist

My suggestion meets these guidelines:

  • This wouldn’t be a breaking change in existing TypeScript/JavaScript code
    • Shouldn’t be any. Anything that expects the old signature, TypeScript should just infer T to number.
    • Now that I think about it, this make the return type of the function narrows down. So, it could make type inference on a variable declaration changes unexpectedly. An explicit type annotation should fix this.
  • This wouldn’t change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript’s Design Goals.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:12
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
BernardoMarianocommented, Oct 1, 2022

I don’t think annotating the generic as <T extends number> is correct because T don’t necessarily have to extend number, it is stated on the language that:

Returns NaN if any of the parameters is or is converted into NaN.

Meaning that it could be a string as well.

Math.min("5", "6e10") // correctly returns 5
Math.max("5", "6e10") // correctly returns 60000000000
0reactions
MartinJohnscommented, Oct 2, 2022

And since it’s impossible to type literal Infinity (I tried)

Related: #32277

Read more comments on GitHub >

github_iconTop Results From Across the Web

Type Math.min & Math.max using generic #30924 - GitHub
Let's say I have this type: type TBankNoteValues = 1 | 5 | 10 | ...
Read more >
C#: generic math functions (Min, Max etc.) - Stack Overflow
You probably want to constrain the generic types to implement IComparable : public T Max<T>(T v1, T v2) where T: struct, IComparable<T>.
Read more >
Generic Max Min : Math « Development Class « C# / C Sharp
45. Math.Log10 Method returns the base 10 logarithm of a specified number. ; 46. Math.Max Method returns the larger of two 8-bit unsigned...
Read more >
Generics in Golang 1.18, no need to write max / min ... - ITNEXT
As the built-in max function is math package accepts float64, the common fixes that peope use are: Write a wrapper function for math....
Read more >
Min and max functions using Generics in Go (Golang)
In the min() and max() functions we declare a type parameter T with constraints.Ordered constraint. It guarantees that the functions work only ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found