microsoft / microsoft/TypeScript
Cannot infer `thisArg` argument type of `Function.bind` for functions with a `this` type consisting of generic functions
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.3k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 117
Descripción
Bug Report
Consider a function like-
declare function callback<T>(this: CallbackCtx<T>): void;
Where CallbackCtx is an object containing generic function(s)-
interface CallbackCtx<T> {
onsuccess: (x: T) => number;
onerror: (err: any) => void;
}
For such a function, that has its this type set to an object containing generic function(s), Function.bind cannot infer the type of thisArg (from the first overload) correctly.
If the type parameter to .bind is explicitly specified (by substituting the generic function type to be a concrete type) - it works fine.
The type of thisArg inferred by typescript automatically, is CallbackCtx<unknown>.
It should be noted that if CallbackCtx contains only non function-like property(s) - the types are inferred correctly. As seen in this playground
🔎 Search Terms
bind, generic, function
🕗 Version & Regression Information
4.2.3
⏯ Playground Link
Playground link with relevant code
💻 Code
interface CallbackCtx<T> {
onsuccess: (x: T) => number;
onerror: (err: any) => void;
}
declare function callback<T>(this: CallbackCtx<T>): void;
declare const ctx: CallbackCtx<string>;
callback.bind(ctx);
// ^ CallbackCtx<string> is not assignable to CallbackCtx<unknown>
callback.bind<(this: CallbackCtx<string>) => void>(ctx);
// ^ Works fine when manually substituting the generic type of the function
🙁 Actual behavior
The line callback.bind(ctx); produces errors
🙂 Expected behavior
The line callback.bind(ctx); should infer the type of callback correctly by substituting the generic type with the help of the type of ctx and should not produce errors
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 el TypeScript Playground enlazado y el ejemplo callback.bind(ctx), comparándolo con la llamada a .bind tipada explícitamente. Traza cómo se infiere para Function.bind el tipo de this que contiene propiedades de función genéricas. Se considera terminado cuando el contexto inferido usa CallbackCtx<string> a partir de ctx y la llamada sin anotación no produce ningún error.
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