microsoft / microsoft/TypeScript

Infer a tuple type instead of array type when possible

Đang mở
#44,309 14 bình luận 14 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ả

Suggestion

I'd prefer typescript to be more aggressive about inferring tuple types, instead of always defaulting to Array.

This is related to https://github.com/microsoft/TypeScript/issues/6574. I'd like to open up a discussion about this again since that thread is locked, and conversation stopped after a workaround was provided without completely addressing the general issue.

🔍 Search Terms

Typescript, tuple, tuples, map, inference

✅ Viability 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, new syntax sugar for JS, etc.)
  • [*] This feature would agree with the rest of TypeScript's Design Goals.

Existing typescript code could start emitting type errors from this change, so it would make sense to provide this as an option in tsconfig.

⭐ Suggestion

Typescript should infer a type to be a tuple instead of an array when it's possible.

📃 Motivating Example

The specific example I have in mind is for Array.prototype.map(), but there are probably more.

const tuple: [number, number, number] = [1, 2, 3];
const result = tuple.map(value => value + 1);

Here, typescript infers the type of result to be number[] rather than [number, number, number]. This could result in some wonkiness when trying to access values inside the tuple:

const [a, b, c, d] = result; // Typescript doesn't detect an error since result is an Array type.
const e = result[10]; // Typescript doesn't detect an error since result is an Array type.

Both of these errors can be caught if Typescript instead inferred result to be the tuple type [number, number, number].

💻 Use Cases

The above example is a use case. I want typescript to tell me when I make errors in my code.

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 bằng việc xem xét hành vi suy luận của TypeScript đối với Array.prototype.map() và cuộc thảo luận trước đó trong issue #6574. Xác định khi nào tuple được ánh xạ nên giữ lại kiểu tuple của nó, bao gồm tùy chọn tsconfig được đề xuất, và thêm các bài kiểm thử cho thấy việc truy cập tuple không hợp lệ bị từ chối, trong khi các kết quả được ánh xạ hợp lệ vẫn tiếp tục hoạt động.

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

Đánh giá

Công nghệ
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
25/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.