microsoft / microsoft/TypeScript
Type argument with a subset of the parameter constraint's optional keys incorrectly reported as unassignable to constraint
Personne n'a encore pris cette issue.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.3k
- Merge moyen
- 2 j 4 h
- PR mergées (30 j)
- 132
Description
🔎 Search Terms
"does not satisfy the constraint" "keyof" "optional"
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about "constraint"
⏯ Playground Link
💻 Code
type MyType<T, U extends { [K in keyof T]?: unknown }> = U;
class MyClass<T> {
value!: MyType<T, { [K in keyof T & string]?: unknown }>;
}
🙁 Actual behavior
The following error is reported:
Type '{ [K in keyof T & string]?: unknown; }' does not satisfy the constraint '{ [K in keyof T]?: unknown; }'.
Type 'keyof T' is not assignable to type 'keyof T & string'.
Type 'string | number | symbol' is not assignable to type 'keyof T & string'.
Type 'string' is not assignable to type 'keyof T & string'.
Type 'string' is not assignable to type 'keyof T'.
Type 'keyof T' is not assignable to type 'string'.
Type 'string | number | symbol' is not assignable to type 'string'.
Type 'number' is not assignable to type 'string'.
🙂 Expected behavior
No error should be reported because due to the keys being optional, { [K in keyof T & string]?: unknown } is a subtype of { [K in keyof T]?: unknown } for all T.
Additional information about the issue
No response
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
Reproduisez le diagnostic de l’exemple TypeScript Playground lié, puis suivez la vérification des contraintes génériques pour les types mappés avec des propriétés facultatives et des intersected keyof keys. Le travail est terminé lorsque l’exemple se compile sans erreur tout en préservant la vérification correcte des contraintes pour les cas associés.
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é
- Calme
- Clarté
- Plutôt claire
- Accessibilité débutants
- 45/100