microsoft / microsoft/TypeScript

Generic type comparison results in incorrect error when inferring from undefined type

Abierto
#57,203 2 comentarios 1 reacción 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Domain: Error Messages Help Wanted Possible Improvement
Lenguaje dominante
Go
Estrellas
111k
Forks
14.4k
Merge medio
1 d 19 h
PR fusionados (30 d)
117

Descripción

🔎 Search Terms

"generics", "infer undefined type", "not assignable"

🕗 Version & Regression Information
  • This changed between versions 3.6.3 and 3.7.5
⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.4.0-dev.20240128#code/C4TwDgpgBA4hB2EBOBLAxgHgCoD4oF4oBvAKCnKgHpKoBJeANwEMAbFAEy3GhQGcp4Ae2BR2EAGYpE7MhQAeALihYoEOcATt+9Zm07cMU8cigBlPAH4zUJQAYSAXxIlqUAMKCkSCGhEAjCAALJgYUQQBXJAUXGgBROSZfFhAoAFsmYDRAqQBzKByEZHQoUEgoAHdPAGt+NE9vJJASOvheETqvH2AARiU4RFRMeHDUgKQ8QiIHKCZ+fqKhkbGcGKgAOUF4AFp0zOz4PIKB4tLob15wlmB+KVUvTygACjVIXwh2AEpmzbaoDobgAAmPqFQYYYajZATYjTWawUHoDBtVAHFarACqiDkrw07CgAWCoU8qwAsp5oLxXihJGh8gjaacoOdLtcoLcmBUICgkHjkEhPNFXFwygByeZggAsgJwIrZ-CEIlmvBQOXgTD8LGgwEEJW4UDF9PBSyhIoAdKtlHqRRCxrK+AJhDNeMrVerNSUdYyRVKzd9WiIpLoOCDjotIeMCDCnfDQxgpTggA

💻 Code
type Generic<T> = {
    // In this scenario, "InvalidType" is not defined
    x: T extends InvalidType<infer S> ? S : 0
}

// Correct behaviour:
// Exactly matching generic type works correctly
const correct1: Generic<number> = {} as Generic<number>
// Non-matching generic type results in error (expected)
const correct2: Generic<number> = {} as Generic<string>

// Unexpected behavior
// More specific generic type results in a weird error:
// Type 'Generic<42>' is not assignable to type 'Generic<number>'.
//  Type 'number' is not assignable to type '42'.
const invalid: Generic<number> = {} as Generic<42>
🙁 Actual behavior

TS error:

Type 'Generic<42>' is not assignable to type 'Generic<number>'.
  Type 'number' is not assignable to type '42'.
🙂 Expected behavior

There should be no TypeScript error, or at least not a confusing error message like that.

It is not clear where the actual error comes from (undefined type InvalidType), and the current
error message does not really make sense - to a user it looks like the type comparison (42 -> number) was inverted (number -> 42).

Additional information about the issue

This also seems to happen in more complex scenarios where all involved types are correctly defined but another TS error like Type instantiation is excessively deep and possibly infinite occurs somewhere in the chain.

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 vinculada en TypeScript Playground y compara el comportamiento reportado entre las versiones mencionadas, 3.6.3 y 3.7.5. Sigue el recorrido de comprobación de tipos para Generic cuando InvalidType no está definido, incluido el escenario adicional de instanciación profunda; se considera terminado cuando el ejemplo no produce ningún error o produce un diagnóstico claro y correctamente orientado.

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

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.