microsoft / microsoft/TypeScript

Conjunction of two disjunctions cause incorrect errors

Abierto
#30,648 4 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Experience Enhancement Suggestion
Lenguaje dominante
Go
Estrellas
111k
Forks
14.4k
Merge medio
1 d 19 h
PR fusionados (30 d)
117

Descripción

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:

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Empieza reproduciendo el ejemplo de conjunction del issue en TypeScript Playground, especialmente try2 y try3, e inspecciona el comportamiento de comprobación de tipos y de diagnóstico del compilador. Se considera terminado cuando el error de try2 apunta a numberWhenGreenTrue o green en lugar de a callbackWhenSmallIsFalsy, mientras que los demás casos mostrados mantienen su comportamiento esperado.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
typescript
Área
compilers
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.