microsoft / microsoft/TypeScript

enhance: Add tests for premature caching of contextual parameter types

Đang mở Phù hợp với người mới
#64,278 1 bình luận 1 reaction 0 người được giao Xem trên GitHub

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

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ả

### Acknowledgement

- [x] I acknowledge that issues using this template may be closed without further explanation at the maintainer's discretion.

### Comment

Add tests for premature caching of contextual parameter types

The checker currently handles the following code correctly.

But the existing cases in `contextuallyTypedParametersWithInitializers2.ts` do not appear to cover the following case: the combination of premature type caching and parameter whose type is widened during contextual checking.

**Example:**

It is ok:

```ts
const fn: (reader: () => number, value: 1) => void =
(get = () => x, x = 0) => {
x.toFixed();
};
```
**Why this needs regression coverage:**

when caching the symbol type, we need to check if it is context sensitive, as the author intentionly said:
```go
func (c *Checker) getTypeOfVariableOrParameterOrProperty(symbol *ast.Symbol) *Type {
links := c.valueSymbolLinks.Get(symbol)
if links.resolvedType == nil {
t := c.getTypeOfVariableOrParameterOrPropertyWorker(symbol)
if t == nil {
panic("Unexpected nil type")
}
// For a contextually typed parameter it is possible that a type has already
// been assigned (in assignTypeToParameterAndFixTypeParameters), and we want
// to preserve this type. In fact, we need to _prefer_ that type, but it won't
// be assigned until contextual typing is complete, so we need to defer in
// cases where contextual typing may take place.
if links.resolvedType == nil && !c.isParameterOfContextSensitiveSignature(symbol) {
links.resolvedType = t
}
return t
}
return links.resolvedType
}
```

Checking `get = () => x` queries `x` before its contextual parameter checking is complete:

1. The initial query get the contextual type `1`.
2. Later, checking the default value `0` and widens the parameter's type to `number`.
3. If the initial result `1` is cached, `assignParameterType` returns early and cannot assign the final type.

This would incorrectly produce the error:

```text
Type '0' is not assignable to type '1'.
```

The existing guard in `getTypeOfVariableOrParameterOrProperty` prevents this premature caching:

```go
!c.isParameterOfContextSensitiveSignature(symbol)
```

The example passes with the current implementation. Removing the guard will produce the incorrect TS2322 diagnostic.

**Proposed change:**

Just add this case to `contextuallyTypedParametersWithInitializers2.ts`

```ts
const test4: (reader: () => number, value: 1) => void =
(get = () => x, x = 0) => {
x.toFixed();
};
```
Add this case to the contextual parameter initializer tests, together with type and symbol baselines, to protect the existing behavior against regressions.

Existing tests cover the caching guard’s effect on implicit any diagnostics. This test adds coverage of type widening for parameters with default initializers, ensuring that prematurely cached literal types do not cause spurious errors.

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 contextuallyTypedParametersWithInitializers2.ts và các trường hợp trình khởi tạo tham số theo ngữ cảnh hiện có, sau đó chạy các bài kiểm thử trình biên dịch liên quan. Thêm trường hợp test4 được đề xuất và cập nhật các baseline về kiểu và symbol của nó. Được xem là hoàn tất khi bài kiểm thử vượt qua với guard bộ nhớ đệm hiện tại và không tạo ra chẩn đoán TS2322 không chính đáng.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
go, typescript
Lĩnh vực
compilers, testing-qa
Loại issue
Lỗi
Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức độ hoạt động
Sôi nổi
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
78/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.