microsoft / microsoft/TypeScript

`extends any ?` may return right hand in conditional types

Abierto
#41,349 2 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Needs Investigation
Lenguaje dominante
Go
Estrellas
111k
Forks
14.3k
Merge medio
2 d 4 h
PR fusionados (30 d)
132

Descripción

TypeScript Version: 4.0.5

Search Terms:

  • type inference
  • extends any
  • conditional types
  • right hand

Expected behavior:

type D1 = X1 <O1> returns false.

Actual behavior:

In the code below, type D1 = X1<O1> seems to return false, but it actually returns true.
If you change the seemingly irrelevant right-hand type in extends any ? written for union distribution to undefined, it will behave as intended.

Related Issues:

Code

type X1<T> = IfEq<
  AllKeys1<T>,
  keyof T,
  true,
  false
>;

type X2<T> = IfEq<
  AllKeys2<T>,
  keyof T,
  true,
  false
>;

type IfEq<X, Y, T, E> = [X] extends [Y] ? ([Y] extends [X] ? T : E) : E;

// AllKeys<{ x: ... } | { x: ...; y: ... }> -> "x" | ("x" | "y") -> "x" | "y"
type AllKeys1<O extends Object> = O extends any ? keyof O : never;
type AllKeys2<O extends Object> = O extends any ? keyof O : undefined;

type O1 = { x: string; y: number } | { x: boolean; z: number }
type A1 = AllKeys1<O1> // "x" | "y" | "z"
type A2 = AllKeys2<O1> // "x" | "y" | "z"
type B = keyof O1 // "x"

type C1 = IfEq<A1, B, true, false> // false
type C2 = IfEq<A2, B, true, false> // false
type D1 = X1<O1> // true ???
type D2 = X2<O1> // false

type O2 = { x: string; y: number } | { x: boolean; y: Object }
type E1 = X1<O2> // true
type E2 = X2<O2> // true
Output
"use strict";

Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "alwaysStrict": true,
    "esModuleInterop": true,
    "declaration": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "moduleResolution": 2,
    "target": "ES2017",
    "jsx": "React",
    "module": "ESNext"
  }
}

Playground Link: Provided

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

Comienza con la reproducción proporcionada en Playground usando TypeScript 4.0.5 y compara los resultados de los tipos condicionales para D1, D2, E1 y E2. Sigue el procesamiento de los tipos condicionales distributivos por parte del compilador y verifica que los resultados esperados y reales informados se resuelvan de forma coherente sin cambiar los demás casos.

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
38/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.