microsoft / microsoft/TypeScript

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

Đang mở
#41,708 7 bình luận 7 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Awaiting More Feedback Suggestion
Ngôn ngữ chính
Go
Star
111k
Fork
14.4k
Merge trung bình
1 ngày 19 giờ
Pull request đã merge (30 ngày)
117

Mô tả

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.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu từ khai báo TypeScript cho Array.prototype.sort và so sánh tham số compareFn của nó với hành vi ECMAScript được mô tả trong issue. Kiểm tra kiểu có điều kiện được đề xuất ảnh hưởng như thế nào đến các mảng chứa undefined, việc suy luận callback và các kiểu hiện có, sau đó xác thực ví dụ được đề xuất. Hoàn thành khi callback dự kiến được chấp nhận mà không thay đổi hành vi khi chạy hoặc gây ra các vấn đề tương thích ngoài dự kiến.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
javascript, typescript
Lĩnh vực
compilers
Loại issue
Tính năng
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
30/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.