microsoft / microsoft/TypeScript

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

Aperta
#43,643 2 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Needs Proposal Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con il TypeScript Playground collegato e riproduci il risultato dell’inferenza per Observable passato a somethingThatTakesSubscribable. Analizza come gli overload separati, incluse le firme deprecate, vengono confrontati con Subscribable. Il lavoro è completato quando lo stesso esempio inferisce result come number invece di unknown, con coverage per il pattern di overload segnalato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.