microsoft / microsoft/TypeScript

"Used before initialization" error false positives and negatives with optional properties

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

@johnfav03 đang làm issue này rồi.

Từ ngày 6/8/2025.

Bug Domain: check: Control Flow Help Wanted
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

used before its initialization

### 🕗 Version & Regression Information

- This changed between versions 4.2.3 and 4.3.5
- This changed in commit or PR #43100 ([`0967f3b`](https://github.com/microsoft/TypeScript/pull/43100/commits/0967f3bac1d82b61d93090eed8e81aa401426d82))

### ⏯ Playground Link

https://www.typescriptlang.org/play/?useDefineForClassFields=true&ts=5.8.3#code/MYGwhgzhAEDC0G8BQ1XTNAvNALgCwEsIA6AIwG4U1SB+ALmgDsBXAW1IFMAnLaARgBMAZkoBfJElCQYAEURVUYBi3bdoAH2jNGAEw4AzAow47KaaKV74ixDNkEik4oA

### 💻 Code

```ts
class C {
a = this.b;
b?: number = 123;
}

class D {
a: number | undefined;
b = this.a = 123;
}
```

### 🙁 Actual behavior

When initializing `C.a`: no error.

When initializing `D.b`:

```
Property 'a' is used before its initialization. (2729)
```

### 🙂 Expected behavior

When initializing `C.a`:

```
Property 'b' is used before its initialization. (2729)
```

When initializing `D.b`: no error.

### Additional information about the issue

Tried with both `useDefineForClassFields` set to `true` and `false`.

The behavior is as expected when switching the use of `undefined` and the "optional" `?` modifier like this:

```ts
class C {
a = this.b;
b: number | undefined = 123;
}

class D {
a?: number;
b = this.a = 123;
}
```

#43100 seems to have taken a wrong approach. Instead of basing the decision to report an error on whether the `?` modifier is present, it should be based on whether the property access is on the left-hand side of an assignment expression.

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.

Đánh giá

Issue này chưa được đánh giá.

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.