-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Type Math.min & Math.max using generic #30924
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
Comments
We created our own |
it would be very nice if you shared these types you created |
I don't think annotating the generic as
Meaning that it could be a Math.min("5", "6e10") // correctly returns 5
Math.max("5", "6e10") // correctly returns 60000000000 |
@BernardoMariano With that argument you can scrap TypeScript completely, because basically all of JavaScript has well-defined results for operations, e.g. for |
I agree with @MartinJohns as TS is supposed to make app as type-safe as possible and as type-sane as possible. That being said, I don't agree with @peat-psuwit 's motion to change |
An overload trivially resolves that. Of course this shouldn’t be the only typing.
It is always accurate. Why shouldn’t it be as accurate as possible?
Yes. It might not be a top priority, but again, why shouldn’t things be accurate? |
Hmm... interesting. My proposal addresses 1 or more argument and exactly 0, but forgot 0 or more case. I think
should handle it. Because if you expect 0 arguments as a possibility, a return value of [1] Is there a proposal somewhere which allow typing literal |
Related: #32277 |
Any updates on this? I'm currently having to use type assertion ( |
Search Terms
Suggestion
Currently, Math.min is typed as:
However, I would like to change the definition to:
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 forInfinity
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 literalInfinity
type.)(The same applies with Math.max, except
Infinity
is now-Infinity
)Use Cases
Let's say I have this type:
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.
And now I can't pass maxValues to a function that expects TBankNoteValues anymore.
Examples
Playground link
Checklist
My suggestion meets these guidelines:
Shouldn't be any. Anything that expects the old signature, TypeScript should just inferT
tonumber
.The text was updated successfully, but these errors were encountered: