microsoft / microsoft/TypeScript

Interface merging for any property names does not allow type narrowing

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

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

Awaiting More Feedback Suggestion
Ngôn ngữ chính
Go
Star
111k
Fork
14.4k
Merge trung bình
1 ngày 19 giờ
Pull request đã merge (30 ngày)
117

Mô tả

TypeScript Version: 4.1.0-dev.20201002

Search Terms: interface merging merge object any name subsequent property declarations

Code

interface PropsContainer {
  props: {
    [propName: string]: any;
  }
}

interface PropsContainer {
  props: {
    [propName: string]: any;
    specialProp: string;
  }
}

Expected behavior:
This should be allowed. The merged interface should result in a type with props that have any prop name as any except for specialProp which is a string.

This would be useful in a case where a third party library (such as @types/react-test-renderer) declare an object type with { [propName: string]: any } syntax, but you want to narrow the value of the property for specific types via interface merging.

Note that even { specialProp?: any, [propName: string]: any } is not allowed even though the types are functionally identical in this case.

Actual behavior:
This results in a Subsequent property declarations must have the same type.

Playground Link: https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgApQPYAcDOBhDcOUaZAbwChlktNcAucq65AbVuwDk4BbCRnGCigA5gF1GcEAE8A3MwC+FJRVCRYiFOmz5CYYiFKVqHBkxbIcWCAmBwANtqwB+STPkX2dbnwFDREshScorKQA

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 reproduction TypeScript Playground được liên kết và xác nhận diagnostic cho hai khai báo PropsContainer được hợp nhất. Sau đó, lần theo đường đi của quá trình kiểm tra kiểu cho các khai báo thuộc tính và index signature tiếp theo. Được xem là hoàn tất khi merge được hiển thị được chấp nhận, specialProp được thu hẹp thành string và các khai báo lại không tương thích hiện có vẫn bị từ chối.

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
Đặc tả 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.