microsoft / microsoft/TypeScript

TypeScript unable to infer types properly between interfaces and overloaded methods.

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

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

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

Mô tả

Bug Report

In all versions of TS I can find, there's no way to mark certain call patterns of a method as deprecated while maintaining inference with certain interfaces. If an interface is split out into different overloads so certain overloads can be deprecated, then it breaks inference in some cases.

🔎 Search Terms

I'm not sure what to search for here. Interfaces?

🕗 Version & Regression Information
  • All versions
⏯ Playground Link

Playground link with relevant code

💻 Code
interface Observer<T> {
    next(value: T): void;
    error(error: any): void;
    complete(): void;
}

interface Unsubscribable {
    unsubscribe(): void;
}

interface Subscribable<T> {
    subscribe(observer?: Partial<Observer<T>>): Unsubscribable;
}

class Observable<T> implements Subscribable<T> {
    /////////////////////////////////////////
    // Valid call pattern
    /////////////////////////////////////////
    subscribe(observerOrNext?: Partial<Observer<T>> | ((value: T) => void)): Unsubscribable;


    //////////////////////////////////////////
    // Deprecated call patterns
    //////////////////////////////////////////
    /**
     * @deprecated This call pattern is going away
     */
    subscribe(next: null | undefined, error: null | undefined, complete: () => void): Unsubscribable;
    /**
     * @deprecated This call pattern is going away
     */
    subscribe(next: null | undefined, error: (error: any) => void, complete?: () => void): Unsubscribable;
    /**
     * @deprecated This call pattern is going away
     */
    subscribe(next: (value: T) => void, error: null | undefined, complete: () => void): Unsubscribable;
    /**
     * @deprecated This call pattern is going away
     */
    subscribe(next?: (value: T) => void, error?: (error: any) => void, complete?: () => void): Unsubscribable;


    ////////////////////////////////////////////
    // Impl
    ////////////////////////////////////////////
    subscribe(...args: any[]): Unsubscribable {
        return {
            unsubscribe() {}
        };
    }
}


function somethingThatTakesSubscribable<T>(arg: Subscribable<T>): T {
    return null!;
}

/////////////////////////////////////
// Our failed expectation here
// (it's unknown)
/////////////////////////////////////
const result = somethingThatTakesSubscribable(new Observable<number>()); // $ExpectType number
🙁 Actual behavior

result is unknown above.

🙂 Expected behavior

result would be number.

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 TypeScript Playground được liên kết và tái hiện kết quả suy luận cho Observable được truyền vào somethingThatTakesSubscribable. Điều tra cách các overload được tách riêng, bao gồm cả các signature đã lỗi thời, được so sánh với Subscribable. Hoàn thành khi cùng một ví dụ suy luận result là number thay vì unknown, với coverage cho pattern overload được báo cáo.

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.