microsoft / microsoft/TypeScript
ThisType is invalid when using object spread on generic object
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
ThisType Object Spread Generic
🕗 Version & Regression Information
- I was unable to test this on prior versions because I was not using this feature previously
⏯ Playground Link
Playground link with relevant code
💻 Code
function createObject<D extends {}>(def: D & ThisType<D>) { /* some code */ }
const namableObject = { name: '' };
// ThisType will be wrongly typed when using object spread
const spreadObject = createObject({
...namableObject,
get thisName() {
return this.name; // <-- Property 'name' does not exists on type '{}'
},
});
// Correctly typed otherwise
const fullObject = createObject({
name: '',
get thisName() {
return this.name;
},
});
🙁 Actual behavior
The generic type is broken when the given object value contains a spread object.
🙂 Expected behavior
The generic type should contain the spreaded object properties as those are finally included into the result object.
Comment
This may be related to #26412 and other issues like this one but I didn't found a workarround to get a ThisType correctly typed.
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 la reproduction liée dans TypeScript Playground et comparez les appels à createObject avec et sans spread afin de voir comment l’inférence générique gère l’objet spread et le contexte ThisType ; c’est terminé lorsque le getter de spreadObject reconnaît la propriété name sans provoquer de régression dans le cas existant fullObject.
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