microsoft / microsoft/TypeScript
suggest `in` operator when accessing non-common property of union types
Chưa có ai nhận issue này.
- 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ả
🔍 Search Terms
access to non-common property of union types, suggest in operator for union types
✅ Viability Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
⭐ Suggestion
Background: https://twitter.com/kentcdodds/status/1420125238436655104?s=21
When accessing props that are not on all union types, tsc gives Property 'foo' does not exist on type 'XX', which is confusing to newcomers. We can suggest the in operator when it is used inside if condition.
📃 Motivating Example
type A = {one: string}
type B = {two: string}
type C = A | B
declare const thing: C
// 😕Before:
// Property 'two' does not exist on type 'C'.
// Property 'two' does not exist on type 'A'.(2339)
if (thing.two) {
// it's a B
}
// 😄After:
// Property 'two' does not exist on type 'C'.
// Property 'two' does not exist on type 'A'.(2339)
// Did you mean to use `'two' in thing`?
if (thing.two) {
// it's a B
}
💻 Use Cases
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu bằng cách xác định phần triển khai và các bài kiểm thử của trình biên dịch TypeScript cho diagnostic 2339 và các diagnostic gợi ý liên quan. Xác nhận hành vi của việc truy cập thuộc tính bên trong điều kiện if, sau đó thêm gợi ý được đề xuất cho toán tử in với các bài kiểm thử bao quát union type và xác minh cách diễn đạt của diagnostic.
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
- Tính năng
- Độ 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