microsoft / microsoft/TypeScript

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

Open
#43,643 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Needs Proposal Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked TypeScript Playground and reproduce the inference result for Observable passed to somethingThatTakesSubscribable. Investigate how the split overloads, including the deprecated signatures, are compared with Subscribable. Done means the same example infers result as number rather than unknown, with coverage for the reported overload pattern.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.