microsoft / microsoft/TypeScript

'in' does not remove undefined from indexed value (with noUncheckedIndexedAccess)

Đang mở
#61,389 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: check: Control Flow 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

in operator, noUncheckedIndexedAccess

### 🕗 Version & Regression Information

Tested 5.8.2 and 5.9.0-dev.20250310

### ⏯ Playground Link

https://www.typescriptlang.org/play/?noUncheckedIndexedAccess=true&ts=5.8.2#code/MYewdgzgLgBAhgJwQLhmArgWwEYFMEDaAujALwwECMANDAEy0DMRA3AFCiSwCWAJgB6oMOfGXrtuAMxgAKPvxjcw8JAEoYAbzYwdMTtBiC0WPAjGJC81tt36QAG1wA6eyADmM-qvYBfNkA

### 💻 Code

With an array:
```ts
const arr: number[] = [1, 2, 3];
const idx: number = 2;
if (idx in arr) {
const x: number = arr[idx];
console.log(x);
}
```

With a map/object:
```ts
const map: Record = { a: 1 };
const key: string = "a";
if (key in map) {
const x: number = map[key];
console.log(x);
}
```

### 🙁 Actual behavior

```
Type 'number | undefined' is not assignable to type 'number'.
Type 'undefined' is not assignable to type 'number'.
```

### 🙂 Expected behavior

Expected 'in' operator to guarantee that the result of indexing is not `undefined` in this branch.

### Additional information about the issue

_No response_

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

Start with the linked TypeScript Playground reproduction using noUncheckedIndexedAccess and both the array and Record examples. Trace how the type checker narrows indexed access inside the `in` branches. Done means both examples allow assigning the indexed values to number without weakening the option’s existing behavior.

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ó
4/5
Thời gian dự kiến
3-5 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
35/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.