microsoft / microsoft/TypeScript

Suggestion: Array.prototype.sort - remove the need for compareFn to handle undefined values

Offen
#41,708 7 Kommentare 7 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

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).

all undefined elements are sorted to the end of the array, with no call to the compareFunction
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

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
sort(compareFn?: (a: T, b: T) => number
Suggested
sort(compareFn?: (a: T extends undefined ? never : T, b: T extends undefined ? never : T) => number

Use Cases / Examples

The below which is not currently valid would now be valid.

[1, undefined, 2].sort((a: number, b: number) => a - b)

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • 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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginnen Sie bei der TypeScript-Deklaration für Array.prototype.sort und vergleichen Sie deren compareFn-Parameter mit dem im Issue beschriebenen ECMAScript-Verhalten. Prüfen Sie, wie sich der vorgeschlagene bedingte Typ auf Arrays auswirkt, die undefined enthalten, auf die Inferenz von Callbacks und auf bestehende Typisierungen, und validieren Sie anschließend das vorgeschlagene Beispiel. Erledigt ist die Aufgabe, wenn der vorgesehene Callback akzeptiert wird, ohne das Laufzeitverhalten zu ändern oder unbeabsichtigte Kompatibilitätsprobleme einzuführen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, typescript
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
30/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.