microsoft / microsoft/TypeScript

union of pattern template literals intersected with optional brand mutually assignable to one with incompatible brand

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

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

Bug Domain: Intersection
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

brand, flavor, pattern template literal, placeholder, union, intersection, optional, assignable

🕗 Version & Regression Information
  • This changed between versions 4.3.0-dev.20210330 and 4.3.0-dev.20210331
  • This changed in commit or PR #43440, I think
⏯ Playground Link

Playground link

💻 Code
type TmpLitUnion = `0${string}` | `1${string}`
type BrandX = TmpLitUnion & { a?: "x" };
type BrandY = TmpLitUnion & { a?: "y" };
declare const brandY: BrandY;
const brandX: BrandX = brandY; // <-- no error
🙁 Actual behavior

BrandX and BrandY are mutually assignable, even though they have an incompatible optional a property.

🙂 Expected behavior

There should be an error when trying to assign a BrandY to a BrandX or vice versa.

Additional information about the issue

This is from a Stack Overflow question. Looks like optional brands (this is called... "flavored" I guess?) don't distinguish between unions-of-pattern-template-literals. If you get rid of the union, you get the expected error:

type TmpLit = `0${string}`;
type BrandX = TmpLit & { a?: "x" };
type BrandY = TmpLit & { a?: "y" };
declare const brandY: BrandY;
const brandX: BrandX = brandY; // <-- error

Or if you add something else to the union, you get the expected error:

type TmpLitUnionOrSomethingElse = `0${string}` | `1${string}` | "somethingElse"
type BrandX = TmpLitUnionOrSomethingElse & { a?: "x" };
type BrandY = TmpLitUnionOrSomethingElse & { a?: "y" };
declare const brandY: BrandY;
const brandX: BrandX = brandY; // <-- error

Or if the brand is not optional, etc.

Not sure if this is a bug or a design limitation, or somehow intentional.

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

Tái hiện phép gán trong TypeScript Playground được liên kết, sau đó so sánh với các ví dụ không có union và không có brand tùy chọn trong issue. Điều tra tính có thể gán của các union gồm các template literal theo pattern được giao với các thuộc tính tùy chọn; được xem là hoàn thành khi BrandX và BrandY không còn có thể gán cho nhau, đồng thời các trường hợp so sánh được báo cáo vẫn chính xác.

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
Ít trao đổi
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
52/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.