microsoft / microsoft/TypeScript

narrowing to "never" by "if" statement does not exclude "undefined" return type

Đang mở
#21,985 3 bình luận 28 reaction 0 người được giao Xem trên GitHub

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

In Discussion 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: 2.7.1

Search Terms: narrowing types never switch case if undefined strictNullChecks

Code

// Build this code with "--strictNullChecks" option.

function assertNever(x: never): never {
    throw new Error("not reached");
}

enum A {
    Foo, Bar
}

// There are no errors on this function.
function good(a: A): number {
    switch (a) {
        case A.Foo: return 0;
        case A.Bar: return 1;
    }
}

// This function causes the following error though "a" is "never" at the end of the function.
// error TS2366: Function lacks ending return statement and return type does not include 'undefined'.
function bad(a: A): number {
    if (a === A.Foo) return 0;
    if (a === A.Bar) return 1;
}

// No error if explicit return of "never" is added.
function ok(a: A): number {
    if (a === A.Foo) return 0;
    if (a === A.Bar) return 1;
    return assertNever(a);
}

Expected behavior: The bad() function should not cause error so that if statement works like switch.

Actual behavior: As commented in the code, only bad() function causes an error.

Playground Link: (please check "strictNullChecks" option.)

http://www.typescriptlang.org/play/#src=function%20assertNever(x%3A%20never)%3A%20never%20%7B%0D%0A%20%20%20%20throw%20new%20Error(%22unexpected%22)%3B%0D%0A%7D%0D%0A%0D%0Aenum%20A%20%7B%0D%0A%20%20%20%20Foo%2C%20Bar%0D%0A%7D%0D%0A%0D%0Afunction%20good(a%3A%20A)%3A%20number%20%7B%0D%0A%20%20%20%20switch%20(a)%20%7B%0D%0A%20%20%20%20%20%20%20%20case%20A.Foo%3A%20return%200%3B%0D%0A%20%20%20%20%20%20%20%20case%20A.Bar%3A%20return%201%3B%0D%0A%20%20%20%20%7D%0D%0A%7D%0D%0A%0D%0Afunction%20bad(a%3A%20A)%3A%20number%20%7B%0D%0A%20%20%20%20if%20(a%20%3D%3D%3D%20A.Foo)%20return%200%3B%0D%0A%20%20%20%20if%20(a%20%3D%3D%3D%20A.Bar)%20return%201%3B%0D%0A%7D%0D%0A%0D%0Afunction%20ok(a%3A%20A)%3A%20number%20%7B%0D%0A%20%20%20%20if%20(a%20%3D%3D%3D%20A.Foo)%20return%200%3B%0D%0A%20%20%20%20if%20(a%20%3D%3D%3D%20A.Bar)%20return%201%3B%0D%0A%20%20%20%20return%20assertNever(a)%3B%0D%0A%7D

Related Issues:

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 bằng cách tái hiện mã TypeScript của issue với strictNullChecks được bật và so sánh các trường hợp if và switch. Theo dõi quá trình thu hẹp control-flow của compiler và phân tích missing-return đối với hàm bị lỗi. Hoàn tất khi diagnostic được báo cáo đã được giải quyết cho trường hợp này và regression coverage xác nhận hành vi.

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.