microsoft / microsoft/TypeScript
Two way assignability condition with generic params is not working as expected
Personne n'a encore pris cette issue.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.4k
- Merge moyen
- 1 j 19 h
- PR mergées (30 j)
- 117
Description
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
}
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par le TypeScript Playground lié et la reproduction minimale de NumberIfExtends<G, G>. Suivez la manière dont l'assignabilité des types conditionnels avec des paramètres génériques est vérifiée, en comparant la forme alias aux exemples inline et à sens unique. C'est terminé lorsque l'affectation de 1 au type conditionnel générique réussit sans l'erreur signalée.
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é
- Active
- Clarté
- Plutôt claire
- Accessibilité débutants
- 48/100