microsoft / microsoft/TypeScript

Two way assignability condition with generic params is not working as expected

Abierto
#44,975 1 comentario 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Bug Domain: Conditional Types Needs Human Review
Lenguaje dominante
Go
Estrellas
111k
Forks
14.4k
Merge medio
1 d 19 h
PR fusionados (30 d)
117

Descripción

Bug Report

🔎 Search Terms

assignability, ternary, conditions, generics

🕗 Version & Regression Information
  • This is the behavior in every version I tried
⏯ Playground Link

Playground link with relevant code

💻 Code
type NumberIfExtends<T, U> = T extends U ? U extends T ? number : string : string;
function f<G>() {
  const x: NumberIfExtends<G, G> = 1;
}
🙁 Actual behavior

The value 1 is not assignable to variable x, with the following error: Type 'number' is not assignable to type 'NumberIfExtends<G, G>'.

🙂 Expected behavior

This should be assignable, since the the value passed to both T and U generic params is the same.

Please note the following very similar examples, that works without errors.
This is to show this is a minimal reproduction, and that the different do not justify the error:

type NumberIfExtends<T, U> = T extends U ? U extends T ? number : string : string;
const x: NumberIfExtends<'some type', 'some type'> = 1; // if I pass an explicit type (Not a generic)
type NumberIfExtends<T, U> = T extends U ? number : string; // if I use the condition in only one way. 
function f<G>() {
  const x: NumberIfExtends<G, G> = 1;
}
function f<G>(): void {
  const x: G extends G ? G extends G ? number : string : string = 1; // if I define the type without an alias. This is not always possible if instead of defining a variable I would be passing the value to a function who defines its own type
}

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 el TypeScript Playground enlazado y la reproducción mínima de NumberIfExtends<G, G>. Rastrea cómo se comprueba la asignabilidad de los tipos condicionales con parámetros genéricos, comparando la forma de alias con los ejemplos inline y unidireccional. La tarea está terminada cuando asignar 1 al tipo condicional genérico tiene éxito sin el error indicado.

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
Activo
Claridad
Bastante claro
Aptitud para principiantes
48/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.