-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Suggestion: Array.prototype.sort - remove the need for compareFn to handle undefined values #41708
Comments
Not sure if this is classed as a bug fix or a suggestion, so out of caution I have marked it as a suggestion. If this gets approved as an issue then I will open a PR with a fix! |
Making this a conditional type is going to make a lot of things a lot worse in terms of assignability and contextual typing, and honestly this is the first I've ever even heard of this behavior. Doesn't seem like a benefit on net given the current rules around conditional types. |
@RyanCavanaugh Forgive my lack of understanding, but could you please explain how this will make assignability and contextual typing worse? I have looked at the documentation for contextual typing and I don’t understand how it would make things worse. Additionally, I have created a playground to demo the behaviour Playground Link and a more complete example that may help make the benefit more obvious. Example below errors, but would compile with my proposed change
Example that currently compiles, but requires far more code that is never actually called and only leads to confusion.
|
Mmm, maybe it's not as bad as I expected. |
I have a branch with a fix ready for a PR once this gets marked as help wanted/added to the backlog |
A PR is now open with a fix for this |
Hey @RyanCavanaugh is there any chance I could get someone to have a look over this issue and the corresponding pr, for more feedback? |
Search Terms
Array.prototype.sort undefined, Array.prototype.sort, sort undefined, extends undefined ? never : T, Array prototype sort, Array.prototype
Suggestion
Based on my understanding of the JS spec the Array.prototype.sort compareFunction is never called on undefined values in an array (they are always sorted to the end of the array).
However, currently the inputs of the compareFn have the type T where T is the types of values in the array. Which forces the developer to make their compare function handle undefined values, despite the compare function never being called with undefined values.
Current
Suggested
Use Cases / Examples
The below which is not currently valid would now be valid.
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: