microsoft / microsoft/TypeScript

Cannot infer `thisArg` argument type of `Function.bind` for functions with a `this` type consisting of generic functions

Ouverte
#43,239 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Bug Domain: This-Typing
Langage dominant
Go
Étoiles
111k
Forks
14.3k
Merge moyen
2 j 4 h
PR mergées (30 j)
132

Description

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

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par le TypeScript Playground lié et l’exemple callback.bind(ctx), en le comparant à l’appel .bind explicitement typé. Suivez la manière dont le type this contenant des propriétés de fonction génériques est inféré pour Function.bind. C’est terminé lorsque le contexte inféré utilise CallbackCtx<string> provenant de ctx et que l’appel non annoté ne produit aucune erreur.

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

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.