microsoft / microsoft/TypeScript

Allow tuple labels passed to `satisfies` to participate in type inference

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

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

Experience Enhancement Help Wanted Suggestion
Ngôn ngữ chính
Go
Star
111k
Fork
14.3k
Merge trung bình
2 ngày 4 giờ
Pull request đã merge (30 ngày)
132

Mô tả

### 🔍 Search Terms

tuple, tuples, label, labels, tuple labels, satisfies, as const

### ✅ Viability Checklist

- [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
- [x] This wouldn't change the runtime behavior of existing JavaScript code
- [x] This could be implemented without emitting different JS based on the types of the expressions
- [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- [x] This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- [x] This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals

### ⭐ Suggestion

Hey TypeScript team! First of all, _super_ excited to hear about `tsgo`.

I've got a feature request for y'all -- the body of this issue was ported from a playground that I created:

https://tsplay.dev/NljeQW

Basically, I often find myself using the `satisfies` operator to define mutable tuples inline:

```typescript
const base = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] as const satisfies any[]
// ^? const base: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
```

Sometimes when working with tuples of a known length, I use labels, because why not? Free semantics!

But to get the behavior I'm after, I have to add a layer of indirection.

```typescript
const ex = [0, 0] as const
type Example = never | [x: typeof ex[0], y: typeof ex[1]]
const example: Example = [...ex]
// ^? const example: [x: 0, y: 0]
```

This makes the code more confusing to read, which makes the tradeoff basically a wash.

### __Feature Request__

Allow any labels passed to `satisfies` to participate in type inference, like so:

```typescript
const point = [0, 0] as const satisfies [x: any, y: any]
//
// Desired type:
// ^? const point: [x: 0, y: 0]
// ^ ^

```

Let me know if you'd like a more complex example, or if there's anything else I can provide.

### 📃 Motivating Example

Playground:

https://tsplay.dev/NljeQW

### 💻 Use Cases

1. What do you want to use this for?

Adding free semantics

3. What shortcomings exist with current approaches?

Requires a layer of indirection + introduces the possibility of drift

5. What workarounds are you using in the meantime?

Outlined in the example I shared

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 với TypeScript Playground được liên kết và tái hiện các ví dụ liên quan đến satisfies, as const và labeled tuples. Issue không nêu tên tệp nguồn hoặc test nào; hoàn thành có nghĩa là các label được cung cấp thông qua satisfies tham gia vào quá trình suy luận trong khi vẫn bảo toàn các kiểu phần tử tuple được minh họa.

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
38/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.