microsoft / microsoft/TypeScript

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

Abierto
#41,708 7 comentarios 7 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Awaiting More Feedback Suggestion
Lenguaje dominante
Go
Estrellas
111k
Forks
14.3k
Merge medio
1 d 19 h
PR fusionados (30 d)
117

Descripción

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.

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza en la declaración de TypeScript para Array.prototype.sort y compara su parámetro compareFn con el comportamiento de ECMAScript descrito en el issue. Comprueba cómo afecta el tipo condicional propuesto a los arrays que contienen undefined, a la inferencia del callback y a las tipificaciones existentes; después, valida el ejemplo sugerido. Se considera terminado cuando se acepta el callback previsto sin cambiar el comportamiento en tiempo de ejecución ni introducir problemas de compatibilidad no deseados.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
javascript, typescript
Área
compilers
Tipo de issue
Nueva funcionalidad
Dificultad
5/5
Tiempo estimado
Más de una semana
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
30/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.