microsoft / microsoft/TypeScript

3.5 regression: return type forced more restrictive than needed

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

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

Domain: check: Control Flow Domain: Indexed Access Types Needs Proposal 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: 3.5.0-dev.20190508

Search Terms: restrictive return type

Code

class Box<A> {
    constructor(public val: A) {}
}

interface ContainerFor<A> {
    array: Array<A>;
    box: Box<A>;
}

function single<T extends keyof ContainerFor<number>>(t: T, x: number): ContainerFor<number>[T] {
    switch (t) {
        case "array":
            return new Array<number>(x);
        case "box":
            return new Box<number>(x);
    }
    return new Box<number>(0); // this should not be needed
}

function test() {
    let t1 = single("array", 42);
    let t2 = single("box", 42);
    //let t3 = single("boo", 42);
}

Expected behavior:
This code worked with 3.4, but had an unsoundness bug in that single was allowed to return the wrong type (swap array and box). It was clear that switching over t did not increase knowledge about the expected output type and I was forced to have a fallback return which is of course unreachable. But the code itself worked.

The test function also shows things work as expected. The types for t1 and t2 are inferred correctly and asking for an unknown thing ("boo") is reported as an error.

I would expect this to still work in 3.5, perhaps fixing one of the issues I'm having in the internals of single.

Actual behavior:
Typescript 3.5 no longer accepts this code. returning new Array or new Box are flagged as wrong return types. The newly expected return type is now mandated to be the intersection type: number[] & Box<number>, which is too restrictive as the correct output type can be statically determined from T in the input. The fact that test correctly infers the output type is proof of this.

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 ví dụ về hàm generic bằng TypeScript 3.5.0-dev.20190508 và so sánh hành vi của nó với TypeScript 3.4, tập trung vào cách switch trên T xác định kiểu trả về được lập chỉ mục. Được xem là hoàn tất khi các nhánh array và Box được chấp nhận mà không cần fallback không thể đạt tới, trong khi các khóa không hợp lệ vẫn là lỗ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
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.