microsoft / microsoft/TypeScript
ThisType is invalid when using object spread on generic object
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.4k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 117
Descripción
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.
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza con la reproducción enlazada en TypeScript Playground y compara las llamadas a createObject con y sin spread para analizar cómo la inferencia genérica maneja el objeto spread y el contexto ThisType; se considera terminado cuando el getter de spreadObject reconoce la propiedad name sin producir una regresión en el caso existente de fullObject.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- typescript
- Área
- compilers
- Tipo de issue
- Error
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100