microsoft / microsoft/TypeScript

Type variable not inferred correctly unless unused declaration is provided

Abierto
#52,432 1 comentario 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Bug Domain: check: Type Inference Help Wanted
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
  1. Proper inference of the M type variable and this context in the methods section depends on having an unused overloaded version of a function.
  2. Even though M is properly inferred for fun (visible after hovering over the function) and this context in methods after uncommenting the unused declaration, this context in data is still not properly inferred (when hovering over this, 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

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. 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

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.