microsoft / microsoft/TypeScript

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

Ouverte
#57,203 2 commentaires 1 réaction 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Domain: Error Messages Help Wanted Possible Improvement
Langage dominant
Go
Étoiles
111k
Forks
14.4k
Merge moyen
1 j 19 h
PR mergées (30 j)
117

Description

🔎 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.

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par la reproduction liée dans TypeScript Playground et comparez le comportement signalé entre les versions mentionnées, 3.6.3 et 3.7.5. Suivez le chemin de vérification des types pour Generic lorsque InvalidType n’est pas défini, y compris le scénario supplémentaire d’instanciation profonde ; le travail est terminé lorsque l’exemple ne produit aucune erreur ou produit un diagnostic clair et correctement orienté.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
typescript
Domaine
compilers
Type d'issue
Bug
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
42/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.