microsoft / microsoft/TypeScript

Generic TypedArray constructors have only one overload

Đang mở
#61,680 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.

Domain: lib.d.ts Help Wanted Possible Improvement
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

Uint8Array

### 🕗 Version & Regression Information

- This is the behavior in versions 5.8.3 and 5.7.3
- I was unable to test this on prior versions because `Uint8Array` is not generic in earlier versions

### ⏯ Playground Link

https://www.typescriptlang.org/play/?target=11&ts=5.8.3#code/MYewdgzgLgBBIFcBOwCmMC8MyoO4wFUBLMKADgEEkkBDATwAoBGABgEoBuAKC9EllAAHOk0zY8hEuSq1G8ZGk69w0GELoAmMTnzFSlavQA8M+gCEEAM0uokAGSIBrVAD4G8lKiV9V6gMzaEnrShnQmoRbWtm4eitxAA

### 💻 Code

```ts
const source = new Uint8Array(10);

const copy1 = new Uint8Array(source); // OK
const copy2 = new Uint8Array(source); // TS 2345
const copy3 = new Uint8Array(source); // TS 2345
```

### 🙁 Actual behavior

Only the generic constructor overload of `Uint8Array` is available when using `Uint8Array` with a generic parameter, i.e. the one which accepts a `TArrayBuffer` as the first argument. It is therefore not possible to pass a `Uint8Array` as the first argument without a compile error, even though this is valid.

### 🙂 Expected behavior

It should be possible to use all the valid overloads of `Uint8Array` (and friends) constructor without a compiler error.

### Additional information about the issue

This seems to only affect `target` value of `ES2024` or `ESNext`, but I can't figure out why from looking at the types.

I assume this behaviour occurs for the same reason as given in [this comment](https://github.com/microsoft/TypeScript/issues/60745#issuecomment-2538876758). In that case I assume the fix would be adding copies of all the constructor overloads to the `Uint8ArrayConstructor` interface, but with type parameters.

For example, the following works ([TS Playground](https://www.typescriptlang.org/play/?target=11&ts=5.8.3#code/JYOwLgpgTgZghgYwgAgKqjADgIJSnATwGEB7EAZzCgFcEwSpkBvAKGXeQHpPkwALYOWSDkAK2qVkcZAhIAHAshIxkEAB6CwoAOZKAbtAA2JOABNyAGmQB3YP2QADACq58BAELUYMaA+Ry4fABbKVNTCFM2DhAIa2QAHhc8Qk9vaFU1SBBzZFcUrx8oABlgAGsUAF5c5I8C6AA+AAoojmRDCBBtfgAuZBBqIIAjaAsW5ABKXvRwHBrEvNq0qHqAbjGYuPma1MKMrJyFnegS8uQqw7rl5tb2QLdehZOIeP6hhtHWybQMWbcttyOyzWrQ2CSSAMuew6B22lyeZ2qEKWTTG7EGlwesKWHxugwIkAA8t5yBAwAB+XqvYZQHGtdqdfgUvoDam0iZTH4Lf75ZHA6KxMEXJZQ7JCIWFeHnLGFFE3KQ1TFuJ4vFkNZAAH0RPMKbK+0ywXPB2oaawAviwWLIKGBkOQSNQoEgEaD9b9CI0AIwABnGa0tZEksgUHudAtdC0adodSF9-utMnkBAATKG4uG5uLjmUIE0o46ILGrYHEwBmVPfGZczNXPMxtZAA)):

```ts
interface Uint8ArrayConstructor {
// this is just a copy of existing overloads, with `TArrayBuffer` param added
new (
length: number,
): Uint8Array;
new (
array: ArrayLike,
): Uint8Array;
new (
buffer: ArrayBuffer,
byteOffset?: number,
length?: number,
): Uint8Array;
new (
array: ArrayLike | ArrayBuffer,
): Uint8Array;
}

const source = new Uint8Array(10);

const copy1 = new Uint8Array(source);
const copy2 = new Uint8Array(source);
const copy3 = new Uint8Array(source);
```

I think this is basically the opposite problem to that which was fixed by #60934.

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à so sánh hành vi của hàm khởi tạo Uint8Array trong ES2024 và ESNext. Xem lại các khai báo hàm khởi tạo Uint8Array generic được mô tả trong issue, sau đó xác minh rằng các ví dụ về overload length, array-like và buffer biên dịch được với các đối số kiểu tường minh.

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
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
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
42/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.