microsoft / microsoft/TypeScript

Better error messaging for when property assignments fail due to intersection types

Aperta
#42,788 4 commenti 2 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Bug Report

🔎 Search Terms

error messaging
not assignable to type 'never'

🕗 Version & Regression Information

This isn't an error, just a confusing error message for a corner case, so it's been in TypeScript for a while.

⏯ Playground Link

Playground link with relevant code

💻 Code
interface Person {
  name: string;
  age: number;
}

function copyPerson(original: Person, copyTo: Person) {
  for (const key of ["name", "age"] as const) {
    // Type 'string | number' is not assignable to type 'never'.
    //   Type 'string' is not assignable to type 'never'. (2322)
    copyTo[key] = original[key]; // error on copyTo[key]
  }
}

function copyPartialPerson(original: Partial<Person>, copyTo: Partial<Person>) {
  for (const key of ["name", "age"] as const) {
    // Type 'string | number | undefined' is not assignable to type 'undefined'.
    //   Type 'string' is not assignable to type 'undefined'. (2322)
    copyTo[key] = original[key]; // error on copyTo[key]
  }
}

🙁 Actual behavior

The error messages shown in the code above.

🙂 Expected behavior

An error message indicating that the right-hand side of the assignment must be assignable to the intersection of all possible types of the left-hand side of the assignment. The intersection part is specifically what seems to be hard to intuit about this error the first time someone encounters it. The never and undefined types being assigned to in the examples above seem to come out of nowhere.

One option is to add another level of messaging to the error, e.g.

Type 'string | number' is not assignable to type 'string & number'.
  Type 'string | number' is not assignable to type 'never'.
    Type 'string' is not assignable to type 'never'.
Type 'string | number | undefined' is not assignable to type '(string | undefined) & (number | undefined)'.
  Type 'string | number | undefined' is not assignable to type 'undefined'.
    Type 'string' is not assignable to type 'undefined'.

Another option is to special-case assignments that assign to $expr1[$expr2], and have an error message like Cannot assign type $assigneeType to $expr1[$expr2] for all values of $expr2. e.g.

Cannot assign type 'string | number' to 'copyTo[key]' for all possible values of 'key'.
  Type 'string | number' is not assignable to type 'never'.
    Type 'string' is not assignable to type 'never'.
Cannot assign type 'string | number | undefined' to 'copyTo[key]' for all possible values of 'key'.
  Type 'string | number | undefined' is not assignable to type 'undefined'.
    Type 'string' is not assignable to type 'undefined'.

That feels nicer, but we wouldn't want to have that error message any time a bad assignment is made to an index access. To only show the message at the appropriate time, a second check might be necessary, where the type-checker checks if the right-hand side would be assignable to the "read" type of the left-hand side. As in, if the following code would work:

let val = leftHandSideExpression;
val = rightHandSideExpression;

Then we can guess that the user is probably encountering this specific issue.

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 l'esempio collegato di TypeScript Playground e con il comportamento del type-checker per le assegnazioni indicizzate descritto nell'issue. Confronta le diagnostics proposte in stile intersection e quelle per le assegnazioni indicizzate, quindi individua i test e i diagnostics rilevanti del compilatore; l'issue non indica file o test del repository. Il lavoro è completato quando il confuso errore never o undefined spiega il vincolo di intersection senza modificare gli altri errori di accesso tramite indice.

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

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.