microsoft / microsoft/TypeScript

Conditional type which checks nested intersect types evaluates inconsistently when outer type is also intersected and inner type intersects with any.

Abierto
#42,369 4 comentarios 0 reacciones 1 asignado Ver en GitHub

@weswigham ya está trabajando en esto.

Desde el 19/1/2021.

Needs Investigation Rescheduled
Lenguaje dominante
Go
Estrellas
111k
Forks
14.3k
Merge medio
1 d 19 h
PR fusionados (30 d)
117

Descripción

Bug Report

🔎 Search Terms
  • Conditional type
  • union
  • intersect
  • nested
🕗 Version & Regression Information

Confirmed that this issue appears in TypeScript versions 3.7, 4.1, 4.2

⏯ Playground Link

Playground link with relevant code

💻 Code
type IsNotA = { isA: false };
type IsA    = { isA: true };
type A      = IsNotA | IsA;

type IsNotB        = { isB: false };
type IsB           = { isB: true };
type B             = IsNotB | IsB;

type Container<a extends A, b extends B> = {
    traits: a & b;
};

declare const BContainer: Container<IsA, IsB>

type t1 = typeof BContainer['traits'] extends IsB          ? true  : false;
type t2 = typeof BContainer extends Container<any, IsB>    ? true  : false;
type t3 = typeof BContainer['traits'] extends IsNotB       ? false : true;
type t4 = typeof BContainer extends Container<any, IsNotB> ? false : true;

Notice that the types t1, t2, t3, t4 all evaluate to true. Right away it should raise alarms that t4 is evaluated to true because I thought that whenever we intersect a concrete type with any, it is still considered any by the TS compiler even if that behavior itself is an odd choice. Now things get stranger if we change the definition of Container to

type Container<a extends A, b extends B> = {
    traits: a & b;
} & {};

Notice that I intersected on a {} type at the end.

🙁 Actual behavior

t4 now evaluates to false. This definitely has something to do with the traits being a intersect type and the Container taking an any as type parameter, because if we replace the any with IsA or A when instantiating the Container in the extends clause then all evaluate to true again regardless of whether Container has an intersection with {} or not.

🙂 Expected behavior

Intersecting an empty object type {} with Container should not have changed the evaluation of the conditional type.
t4 should either evaluate to true or false in both scenarios. Probably false since it seems a concrete type intersected with any is still any. If TS worked as I would ideally have it then type thing = 0 & any would evaluate to 0 in which case t4 should be true in both cases actually, but TS says type thing = 0 & any is equivalent to any so t4 should always be false.

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.

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.