microsoft / microsoft/TypeScript

Covariant generic parameters fail unification

Ouverte
#61,503 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Bug Domain: check: Type Inference Help Wanted
Langage dominant
Go
Étoiles
111k
Forks
14.3k
Merge moyen
2 j 4 h
PR mergées (30 j)
132

Description

### 🔎 Search Terms

"contravariant", "unification"

### 🕗 Version & Regression Information

- This changed between versions 3.3.3 and 3.5.1

### ⏯ Playground Link

https://www.typescriptlang.org/play/?jsx=0#code/MYewdgzgLgBKZQE4EMBuzEEtkJgXhgAoBKfAPhgG8AoGOcaegWwCNMwBTfGAHgGEoADzKEA+gEYAXEWCSBg0ngpgArgBs1AGhiiATNMKz5i5erXEDJmPPJErKsABMOAM3YdHMZBC9gAngDc1LT0kLBqHBAQovDQWJyeBPwwHIJQHE4+lBFgAOZQABaSqqwciAC+IrLWFjCqGrb1akF0sbBMIIgcMQxI7om8fClpGY5ZOflFJSxl2sAFGHwgzgCCUGLFKqWIFtNllYbSfLVNjWYtoYzp0OLcoKzuhBFRPWHxHtodXa9x-cQXbRg1ygujuIAenEIX26bXejm0z2isL+AN6MEQ3GB4hIQXKwUBoHQWBwsAIJFsNFaaPubE43H4QhEEkstnk2j0LKU1iEtRsXIczjcCS8PhwgWCVLCMERPz6wqSQ1S6UyVAmhU22wOvNsAtc-RFvnFksY0NlcPpipGKuyGUmGpmiDmC0QS1W61E9rKuy2Dq1Rx1Tj1wu8htRUqxYIhHCekSRvThn06MPjKJCgOBoIINMepuRCQRsbNqfKQA

### 💻 Code

```ts
const contravariant = () => {
const combine = (_1: (c:Ctx) => null, _2: (c:Ctx) => null): () => Ctx => () => undefined as any;

const less_constrined = (c: C): null => null;
const more_constrined = (c: C): null => null;
const test1 = combine(less_constrined, more_constrined);
const test2 = combine(more_constrined, less_constrined);
}

const covariant = () => {
const combine = (_1: () => Ctx, _2: () => Ctx): Ctx => undefined as any;

const less_constrined = (): C => undefined as any;
const more_constrined = (): C => undefined as any;
const test1 = combine(less_constrined, more_constrined);
const test2 = combine(more_constrined, less_constrined);
}
```

### 🙁 Actual behavior

In the `contravariant` example the type is inferred from the first argument, so `test1` fails to typecheck, despite both `test1` and `test2` sharing the same argument types, just reversed. This is also different from the `covariant` case where types are properly unified in both `test1` and `test2`

### 🙂 Expected behavior

The behaviour for the covariant and contravariant case should match, so `test1` should be inferred as
`
() => C
`

### Additional information about the issue

_No response_

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 le repro TypeScript Playground lié et comparez les appels contravariants aux appels covariants, y compris le comportement signalé entre les versions 3.3.3 et 3.5.1. L’issue est terminée lorsque les deux ordres d’arguments s’unifient de manière cohérente et que le premier appel contravariant infère le type C plus contraint indiqué dans le comportement attendu.

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

Recevez les nouvelles issues par e-mail

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