microsoft / microsoft/TypeScript

Conjunction of two disjunctions cause incorrect errors

Aperta
#30,648 4 commenti 0 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

TypeScript Version: 3.3

Search Terms:

  • Conjunction

Code

interface Small {
    small: true,
    callbackWhenSmallIsFalsy?: undefined;
}

interface NotSmall {
    small?: false;
    callbackWhenSmallIsFalsy: () => void;
}

interface Green {
    green?: false
    numberWhenGreenTrue?: undefined
}

interface NotGreen {
    green: true
    numberWhenGreenTrue: number;
}

type SmallProps = Small | NotSmall;
type GreenProps = Green | NotGreen;

type Props = SmallProps & GreenProps;

const try1: Props = {
    green: true,
    numberWhenGreenTrue: 5,
    small: true
} // OK

const try2: Props = {
    green: false,
    numberWhenGreenTrue: 5,
    small: true
} // Unexpected Error: 'callbackWhenSmallIsFalsy' is missing...
// Expected Error: 5 is not assignable to undefined for 'numberWhenGreenTrue'

 const try3: Props = {
    green: false,
    small: true
} // OK

 const try4: Props = {
    green: false,
    small: true,
    callbackWhenSmallIsFalsy: () => 5
} // Acceptable Error: Types of property 'small' are incompatible.
// TS can't know which of the conditions to use, so this is okay. NTH: It lists multiple possible errors

Expected behavior:
in try3 I don't expect the error to be about callbackWhenSmallIsFalsy. I would expect an error about green or numberWhenGreenTrue

Actual behavior:
When the conjunction is evaluated as false, it appears that the compiler tries to force an error into the first part of the conjunction, when the falsehood is from the second part of the conjunction.

Playground Link:
http://www.typescriptlang.org/play/#src=interface%20Small%20%7B%0A%20%20%20%20small%3A%20true%2C%0A%20%20%20%20callbackWhenSmallIsFalsy%3F%3A%20undefined%3B%0A%7D%0A%0Ainterface%20NotSmall%20%7B%0A%20%20%20%20small%3F%3A%20false%3B%0A%20%20%20%20callbackWhenSmallIsFalsy%3A%20()%20%3D%3E%20void%3B%0A%7D%0A%0Ainterface%20Green%20%7B%0A%20%20%20%20green%3F%3A%20false%0A%20%20%20%20numberWhenGreenTrue%3F%3A%20undefined%0A%7D%0A%0Ainterface%20NotGreen%20%7B%0A%20%20%20%20green%3A%20true%0A%20%20%20%20numberWhenGreenTrue%3A%20number%3B%0A%7D%0A%0Atype%20SmallProps%20%3D%20Small%20%7C%20NotSmall%3B%0Atype%20GreenProps%20%3D%20Green%20%7C%20NotGreen%3B%0A%0Atype%20Props%20%3D%20SmallProps%20%26%20GreenProps%3B%0A%0Aconst%20try1%3A%20Props%20%3D%20%7B%0A%20%20%20%20green%3A%20true%2C%0A%20%20%20%20numberWhenGreenTrue%3A%205%2C%0A%20%20%20%20small%3A%20true%0A%7D%20%2F%2F%20OK%0A%0Aconst%20try2%3A%20Props%20%3D%20%7B%0A%20%20%20%20green%3A%20false%2C%0A%20%20%20%20numberWhenGreenTrue%3A%205%2C%0A%20%20%20%20small%3A%20true%0A%7D%20%2F%2F%20Unexpected%20Error%3A%20'callbackWhenSmallIsFalsy'%20is%20missing...%0A%2F%2F%20Expected%20Error%3A%205%20is%20not%20assignable%20to%20undefined%20for%20'numberWhenGreenTrue'%0A%0A%20const%20try3%3A%20Props%20%3D%20%7B%0A%20%20%20%20green%3A%20false%2C%0A%20%20%20%20small%3A%20true%0A%7D%20%2F%2F%20OK%0A%0A%20const%20try4%3A%20Props%20%3D%20%7B%0A%20%20%20%20green%3A%20false%2C%0A%20%20%20%20small%3A%20true%2C%0A%20%20%20%20callbackWhenSmallIsFalsy%3A%20()%20%3D%3E%205%0A%7D%20%2F%2F%20Acceptable%20Error%3A%20Types%20of%20property%20'small'%20are%20incompatible.%0A%2F%2F%20TS%20can't%20know%20which%20of%20the%20conditions%20to%20use%2C%20so%20this%20is%20okay.%20NTH%3A%20It%20lists%20multiple%20possible%20errors

Related Issues:

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 riproducendo l’esempio di conjunction dell’issue nel TypeScript Playground, in particolare try2 e try3, e ispeziona il comportamento del compilatore per il controllo dei tipi e la diagnostica. Il lavoro è completo quando l’errore per try2 punta a numberWhenGreenTrue o green invece che a callbackWhenSmallIsFalsy, mentre gli altri casi mostrati mantengono il comportamento previsto.

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.