microsoft / microsoft/TypeScript
Type variable not inferred correctly unless unused declaration is provided
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
The described bug was noticed during debugging of the declarations provided with the Vue framework.
🔎 Search Terms
generic, inference, function, overload
🕗 Version & Regression Information
All 4.x versions available on the playground.
⏯ Playground Link
Playground link with relevant code
💻 Code
interface Methods {
[key: string]: Function;
}
interface Param<M extends Methods> {
data: (this: M) => any;
methods: M;
}
declare function fun<M extends Methods = {}>(
param: Param<M> & ThisType<M>
): void;
// Uncommenting the declaration below results in proper type inference of M
// declare function fun<M extends Methods = {}>(
// param: { unusedProperty: true } & Param<M> & ThisType<M>
// ): void;
fun({
data() {
this.myMethod(); // Even after uncommenting the overloaded version, context is not properly inferred here
},
methods: {
myMethod() {},
mySecondMethod() {
this.myMethod(); // Error without uncommenting the declaration
}
},
});
🙁 Actual behavior
- Proper inference of the
Mtype variable andthiscontext in themethodssection depends on having an unused overloaded version of a function. - Even though
Mis properly inferred forfun(visible after hovering over the function) andthiscontext inmethodsafter uncommenting the unused declaration,thiscontext indatais still not properly inferred (when hovering overthis, it looks as it is inferred to the default{}).
🙂 Expected behavior
M type variable is properly inferred in both data function and methods section without redundant declarations.
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
Empieza ejecutando el ejemplo enlazado de TypeScript Playground y compara la inferencia para las dos declaraciones de fun, centrándote en el genérico M y en los contextos this de data y methods. Como en el issue no se identifica ningún archivo del repositorio ni ningún test, localiza primero las rutas de inferencia del compilador y de tipado contextual antes de añadir un caso de regresión; el trabajo está terminado cuando ambos contextos infieren M sin la sobrecarga redundante.
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