microsoft / microsoft/TypeScript

Suggest specifying generic as union if candidates are different

Aperta
#20,339 10 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Domain: Error Messages Needs Proposal Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

TypeScript Version: 2.6.1.

Code

If a generic type can't be formed by picking one of the inference candidates, you'll get the error you posted.

https://stackoverflow.com/questions/39905523/why-isnt-the-type-argument-inferred-as-a-union-type/39905723#39905723

This makes sense, however I would like to question whether we can improve the user experience around this, so errors due to this constraint are easier to understand and fix.

For example:

{
    function compare<T>(x: T, y: T): number {
        return 1;
    }
    compare(
        'oops',
        /* Argument of type '42' is not assignable to parameter of type 'string'. */
        42,
    );
}

{
    function match<T>(cases: { foo: T; bar: T }): T {
        return cases.foo;
    }
    /*
    Argument of type '{ foo: number; bar: string; }' is not assignable to parameter of type '{ foo: number; bar: number; }'.
        Types of property 'bar' are incompatible.
            Type 'string' is not assignable to type 'number'.
    */
    match({
        foo: 1,
        bar: 'foo',
    });
}

As a TypeScript user, I have struggled with these errors many times, and I've only recently realised the specific constraint on the type system which is the root cause of these errors: generics are picked from the first candidate and are not widened to include all candidates. I have also seen other people struggle with this when learning TypeScript.

We can fix this error by specifying the generic as a union:

    match<string | number>({
        foo: 1,
        bar: 'foo',
    });

However, this fix is really not obvious from the error message, especially if the user is not aware of this constraint on the type system (that generics will not be inferred as unions).

I'm wondering if there's any way we can better surface this constraint to the user, to make it clearer to users how they can fix these type errors, such as by specifying the generic as a union (if that is what they intend).

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

Usa gli esempi compare e match nell'issue come riproduzioni, verificando prima i relativi diagnostics con la versione corrente di TypeScript. Il lavoro è completato quando il diagnostic spiega i diversi candidati di inferenza e chiarisce la correzione con un tipo unione esplicito quando questa è la soluzione prevista.

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

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
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.