Array.prototype.sort make compareFn required in lib.d.ts
See original GitHub issueThis is a suggestion to change the signature declaration of Array sort in lib.d.ts from
sort(compareFn?: (a: T, b: T) => number): this;
to
sort(this: string[], compareFn?: (a: string, b: string) => number): this;
sort(compareFn: (a: T, b: T) => number): this;
i.e. make compareFn non-optional for non-string arrays. This would result in errors in cases such as
[2, 10].sort() // returns [10, 2], i.e. sorted lexicographically, which is very unintuitive.
While this is strictly speaking a breaking change, it wouldn’t be the first time the typings are stricter than the actually definitions.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:14
- Comments:10 (7 by maintainers)
Top Results From Across the Web
Extend native JavaScript array - typescript - Stack Overflow
The compiler asks me to define all Array interface properties. I know if I need this Xarray.prototype = new Array(); , I have...
Read more >typescript-cheatsheet - GitHub Pages
A set of TypeScript related notes used for quick reference. The cheatsheet contains references to types, classes, decorators, and many other TypeScript ...
Read more >TypeScript static code analysis: A compare function should be ...
A compare function should be provided when using "Array.prototype.sort()" ... The default sort order is alphabetic, rather than numeric, regardless of the types ......
Read more >View Library code in Javascript and not TypeScript
and i clicked on sort method to see its syntax, a typescript file opened - lib.es5.d.ts where syntax was shown in typescript.
Read more >Extending JavaScript Arrays with TypeScript
TypeScript's Intellisense is smart enough to combine Array<T> that comes with TypeScript that is defined in core JS file, lib.d.ts and the ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@DanielRosenwasser is going to try this and see what breaks
@DanielRosenwasser dooooo iiiittttt break the world go go go