microsoft / microsoft/TypeScript

Allow narrowing of unions discriminated by numeric literals using `>` `<` etc

Đang mở
#61,770 7 bình luận 0 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.3k
Merge trung bình
2 ngày 4 giờ
Pull request đã merge (30 ngày)
132

Mô tả

🔎 Search Terms

"arguments length"

🕗 Version & Regression Information

It works the same in v3.3.3

⏯ Playground Link

https://www.typescriptlang.org/play/?target=99&module=0&ts=5.8.3#code/GYVwdgxgLglg9mABAUwB4EMC2AHANsgRgAoA6M9AJwHMBnALkQG0wRMAjZCgXUQB8muASkQBvAFCJJiGMERFKtEvjBUoAC0QA+RAAZh4qYcQVkUEBSQKajHVwDcEqQF8UuGslGOjx0+aQA5dH8HQycxMNBIWAQUDBx8ACZScmp6JhZ2Th5+RiFPQxk5KyVkFXV9LyMTMwtEKxt7SpdkNw8Dbx8agKCQ53CxSOh4JDQsPGQAZmSSKwZmVg5uPgEKgtl5VJKyjQBePd1Vjuq-REDgptd3fKPfWvrbXskwpyA

💻 Code

Some basic ways to check if we have any arguments could be these:

function example1(...args: [number] | []) {
    if (args.length > 0) {
        return args[0];
    } else {
        return NaN;
    }
}
function example2(...args: [number] | []) {
    if (args.length) {
        return args[0];
    } else {
        return NaN;
    }
}
function example3(...args: [number] | []) {
    if (args.length === 0) {
        return NaN;
    } else {
        return args[0];
    }
}

But TS doesn't seem to understand the first one.

🙁 Actual behavior

It doesn't understand the way it's done in "example1".

🙂 Expected behavior

It should understand the way it's done in "example1".

Additional information about the issue

Type inference on "args" after the check for "example1":

Image

Type inference on "args" after the check for "example2":

Image

Type inference on "args" after the check for "example3":

Image

Notice how for "example2" and "example3" the type is correctly narrowed to just [number], but for "example1" the unnarowed [number] | [] type remains.

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

Sử dụng các ví dụ TypeScript Playground được liên kết làm điểm bắt đầu; so sánh việc thu hẹp luồng điều khiển cho args.length > 0 với các trường hợp truthiness và equality hiện có. Được xem là hoàn tất khi example1 thu hẹp [number] | [] thành [number] trong nhánh true mà không làm mất hành vi được thể hiện trong examples 2 và 3.

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.