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.

Array.prototype.sort make compareFn required in lib.d.ts

See original GitHub issue

This 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:open
  • Created 6 years ago
  • Reactions:14
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

5reactions
RyanCavanaughcommented, Sep 20, 2017

@DanielRosenwasser is going to try this and see what breaks

3reactions
RyanCavanaughcommented, Aug 15, 2018

@DanielRosenwasser dooooo iiiittttt break the world go go go

Read more comments on GitHub >

github_iconTop 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 >

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