microsoft / microsoft/TypeScript
Some types are incorrectly assignable from a generic distributive conditional type
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
TypeScript Version: 3.5.1
Search Terms: generic conditional distributive assignable from
Code
type Keys<T extends object> = T extends unknown ? keyof T : never;
function f<T extends object>(keys: keyof T, moreKeys: Keys<T>) {
keys = moreKeys;
}
// With this instantiation, `keys` is typed 'a',
// and `moreKeys` is typed 'a' | 'b' | 'c'. The latter
// should not be assignable to the former, but it happens
// in the generic context of the function `f`.
f<{ a: any, b: any } | { a: any, c: any }>('a', 'b');
Expected behavior:
Keys<T> should not be assignable to keyof T since Keys<T> is distributive and will produce a different type than keyof T when T is a union type.
Note that the reverse of this assignment, moreKeys = keys, errors for this exact reason; without a concrete T, it’s hard to tell what the relationship should be, so we simply have no relationship in place.
Actual behavior:
The unsafe assignment is allowed.
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 reproduire l’exemple générique de type conditionnel distributif dans le Playground lié. Suivez le chemin de vérification des types pour l’assignabilité dans le contexte générique, puis ajoutez un test de régression couvrant l’assignation non sûre et le comportement d’assignation inverse indiqué. C’est terminé lorsque l’appel avec 'b' est rejeté, car Keys n’est pas assignable à keyof T.
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é
- Clairement spécifiée
- Accessibilité débutants
- 35/100