microsoft / microsoft/TypeScript

Contextual typing doesn't work when mixing up discriminated unions with function variant

Đang mở
#62,306 6 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: Contextual Types 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

contextual typing, discriminated unions

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries
  • Tested with TS 5.4.5 and TS 5.9.2
⏯ Playground Link

https://www.typescriptlang.org/play/?#code/C4TwDgpgBAcg9sA6nATgawJYDsDmBVLDOLKAXgCgoqoAfKACnoDcBDAGwFcIAuKLDgLYAjCCgCUZAHxQmcDABMxlanQDey6tQwBnAMrAU2HLwNcA3Bs1QAZll6NWnHlG0GjE0tNkKxFzQF9LNUsdfUNcAH5ea3ZtCD9qW3tmdi5efmFRDy85RQt-C3IAY2JXKGAIVwBGXngkVExcAiISUgYWbKhVAuLS4HLK4AAmWoRkdCNm4jIukL03XBMULgAaSyT2zu7yHpKsMorXAGZR+ommwmm29U1QheMbWIg1zQ36DqkuwN2+gdcAFlO40a+EurVmrzsm0+2x65FAkCgwMmYLIQQhViod3CD1M8UskOSjjSLnunW8eUsgU0wVu8xxUUebDiCSobxSTnSghE4k+FN8O0KewOgwArLxkRcWjMblp6UYlqt1lD3ltvhZhf1DsAAGwShoo6XXOZhBVMuIvRIqj6eL6C3r7LWDADs+vOoKNGLZ1rVgqAA

💻 Code
type NotWorkingUnion =
    | ((value: number) => void)
    | {
        isString: true;
        fn: ((value: string) => void);
    }
    | {
    isString?: false;
    fn: ((value: number) => void);
};

const test1: NotWorkingUnion = (a) => {}; // Works
const test2: NotWorkingUnion = { // Works
    isString: true,
    fn: (a) => {}
};
const test3: NotWorkingUnion = { // Works
    isString: false,
    fn: (a) => {}
};
const test4: NotWorkingUnion = { // Doesn't work!
    fn: (a) => {}
};

type WorkingUnion =
    | {
        isString: true;
        fn: ((value: string) => void);
    }
    | {
    isString?: false;
    fn: ((value: number) => void);
};

const test5: WorkingUnion = { // Works
    isString: true,
    fn: (a) => {}
};
const test6: WorkingUnion = { // Works
    isString: false,
    fn: (a) => {}
};
const test7: WorkingUnion = { // Works
    fn: (a) => {}
};
🙁 Actual behavior

Contextual typing breaks when an union is both discriminated by a field and one of the union variants is just a function

🙂 Expected behavior

Contextual typing should correctly work, as the type system should have enough information to discriminate between a function and an object with or without the field

Additional information about the issue

strict mode is enabled.

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 các ví dụ trong TypeScript Playground được liên kết, so sánh test4 với test7 và các trường hợp được phân biệt rõ ràng. Theo dõi cách contextual typing xử lý union chứa một biến thể hàm, sau đó thêm một regression test bao quát trường hợp thiếu discriminant và xác minh rằng tham số callback được suy luậ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
Đì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.