microsoft / microsoft/TypeScript

Generic constraints are "shallow", don't account for depth subtyping

Đang mở
#62,514 2 bình luận 0 reaction 0 người được giao Xem trên GitHub
Docs
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

`ts1360 ts2322 generic fields` , `generic bound subtyping`, `generic constraint subtyping`, `could be instantiated with a different subtype of constraint`, `depth subtyping generics`, `parametric polymorphism depth subtyping`

### 🕗 Version & Regression Information

- This is the behavior in every version I tried, and I reviewed the [documentation for generics](https://www.typescriptlang.org/docs/handbook/2/generics.html) as of 5.9.2

### ⏯ Playground Link

[Playground link](https://www.typescriptlang.org/play/?ts=5.9.2#code/GYVwdgxgLglg9mABAZwIbAKYB4AqiMAeUGYAJsilAE4xgDmAfABQBQAkGKgLYYBclNegBpEAelGIAqmCoYANqmKlEUOIhwA6FgEp+eAN7tZUEFSSceYiQCE4UABb4qVOFWQjUFQgAcM0DKRaAL4sLKCQsAiI4GiYuPhEJOSI+ogAbqhyIHwCtHSIQczsFjnI1Hki4ogAyrByctEy8ooBKmo4ANoARBlZGF0Aulq66t292YMpRhgmZoglVogA6vYAnogwFA6biJlycADuAQD8LCEsVQCiBNzechiIcMCIwKgwWbIsMejYqeM5XQgii6BUQAB8UulMtl+F1SHA6CDCqw2F1gJt7F0dChFJt0RgKIDgeDEHCEV0gA)

### 💻 Code

Errors, as intended:
```ts
function safe(
name: string, // Unrelated to T.
): T {
return name // 'string' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'string'. (2322) 👍
}
```

Does not error, unexpectedly:
```ts
function unsafe(
name: string,
): T["value"] {
return name // No error. Why is this allowed?
}

// Example uncaught error:
unsafe<{ value: "cat" } | { value: "dog" }>(
"fish"
) satisfies "cat" | "dog"
```

### 🙁 Actual behavior

When `T extends string`, TypeScript correctly identifies that an arbitrary other `string` cannot be safely assigned to `T`, because `T` could be instantiated with any subtype of string.

When `T extends { value: string }`, TypeScript incorrectly allows an arbitrary other `string` to be assigned to `T["value"]`, even though `T` could be instantiated with any subtype of `{ value: string }`, and the depth subtyping rule for objects implies that the type of `T["value"]` is also a subtype of `string`.

### 🙂 Expected behavior

- **Both examples should fail with the same error.** It should not be possible to return `name` in the function `unsafe`.

More generally, TypeScript should be consistent in applying subtyping rules to generic constraints `T extends U`. If `U` is a primitive (like `string` or `"A" | "B"`) and `T` is checked as if it could be instantiated with any subtype of `U`, then the same rule should apply when `U` is an object type.
- **If this is working as intended**, the [generics documentation](https://www.typescriptlang.org/docs/handbook/2/generics.html) should be updated to clarify that generic bounds are "shallow," and the correct approach is to use a second generic parameter.

Ideally this disclaimer could go under both the section on [Generic Constraints](https://www.typescriptlang.org/docs/handbook/2/generics.html#generic-constraints) (stipulating that only the top level parameter is treated as generic), as well as [Using Type Parameters in Generic Constraints](https://www.typescriptlang.org/docs/handbook/2/generics.html#using-type-parameters-in-generic-constraints) (to the effect of "note that this is the _only_ way to handle this pattern").

### Additional information about the issue

_No response_

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Hướng nghiên cứu

Bắt đầu với TypeScript Playground được liên kết và so sánh ví dụ string generic với ví dụ indexed-access bằng cách sử dụng union instantiation được hiển thị. Đọc các phần Generic Constraints và Using Type Parameters in Generic Constraints trong tài liệu về generics. Được xem là hoàn tất nếu cả hai ví dụ đều nhận cùng một lỗi phù hợp hoặc tài liệu giải thích rõ hành vi của shallow bound và cách tiếp cận với tham số thứ hai.

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ó
5/5
Thời gian dự kiến
Hơn một tuần
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.