microsoft / microsoft/TypeScript

Suggestion: Distributive code inference "as dist"

Đang mở
#62,631 3 bình luận 0 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.3k
Merge trung bình
2 ngày 4 giờ
Pull request đã merge (30 ngày)
132

Mô tả

### 🔍 Search Terms

- distributive inference
- distributed function
- as dist
- as distributed

### ✅ 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

Add `dist` / `distributed` keyword to enable inference in a distributive manner.

### Concept 1: Inline `as dist`

Handle the inference by distributing over all types used by the expression:

```ts
const transform = (val: [1, 1] | [2, 2]) => [val[0], val[1]] as dist;
typeof transform; // (val: [1, 1] | [2, 2]) => [1, 1] | [2, 2]

//

const add = (...[a, b]: [number, number] | [bigint, bigint]) => a + b as dist;
typeof add; // (...[a, b]: [number, number] | [bigint, bigint]) => number | bigint
```

### Concept 2: Function-level `dist` return type

Allow distribution across an entire function body:

```ts
const f = (...[a, b]: T): dist => {
const sum = a + b; // number | bigint
const arr = [a, b]; // [number, number] | [bigint, bigint];
return {
sum,
arr
} // {sum: number, arr: [number, number]} | {sum: bigint, arr: [bigint, bigint]}
}

const res = f(3,7); // {sum: number, arr: [number, number]}
const rez = f(3n,7n); // {sum: bigint, arr: [bigint, bigint]}

typeof f; // ((...[a, b]: T) => { sum: number; arr: [number, number]; }) & ((...[a, b]: T) => { sum: bigint; arr: [bigint, bigint]; })
```

Concept 1 could also be supported on function expressions with equivalent behavior to Concept 2:

```ts
const f2 = ((...[a, b]: T) => {/*code*/}) as dist; // Equivalent to "f"
```

### Notes

These are early-stage concepts meant to illustrate the core idea. The specific syntax and behaviors should be refined during implementation.

**Open questions:** Interaction with `as const`, generics, nested functions, opt-out mechanisms, etc. will need consideration.
**Potential breaking change:** Codebases with a type called `dist` would conflict, though this would go against TypeScript's PascalCase naming convention for types.

### 📃 Motivating Example

Currently, code is inferred in a non-distributive manner:

> Example inspired by [geon](https://www.reddit.com/user/geon/)
```ts
const transform = (val: [1, 1] | [2, 2]) => [val[0], val[1]] as const;
// Inferred return type: readonly [1 | 2, 1 | 2]
// Expected return type: readonly [1, 1] | readonly [2, 2]
```

This can be addressed by duplicating your code logic within a distributive context in the typesystem:

```ts
const transform = (val: T) => [val[0], val[1]] as T extends unknown
? [T[0], T[1]]
: never
;

const res = transform(undefined! as [1, 1] | [2, 2]); // [1, 1] | [2, 2]
const rez = transform([1, 1]); // [1, 1]
```

But this is very repetitive and awkward. Besides that, this only works if the automatic inference doesn't cause a conflict to begin with and can be simply translated into the type system:

> Example inspired by [Emilio Platzer](https://github.com/emilioplatzer) and Mudloop
```ts
// @ts-expect-error: Operator '+' cannot be applied to types 'number | bigint' and 'number | bigint'.(2365)
const add = (...[a, b]: [number, number] | [bigint, bigint]) => a + b;
```

### 💻 Use Cases

1. What do you want to use this for?
Preserving precise type relationships when transforming union types, and enabling operations across union branches that are currently rejected by the type checker.
2. What shortcomings exist with current approaches?
Current workarounds require manually duplicating code logic in the type system using conditional types. This is verbose, error-prone, and fails when TypeScript rejects the runtime-aligned code itself
3. What workarounds are you using in the meantime?
Manually encoding distributive logic using conditional types

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

Chưa xác định được tệp triển khai, bài kiểm thử hoặc các điểm vào của trình biên dịch. Hãy bắt đầu bằng việc xem xét hai cú pháp được đề xuất, các ví dụ làm động lực và những câu hỏi còn bỏ ngỏ về generics, hàm lồng nhau và opt-outs. Để được xem là hoàn tất, cần chốt design trước khi có thể xác định phạm vi triển khai.

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
Cần làm rõ
Mức phù hợp với người mới
20/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.