microsoft / microsoft/TypeScript

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

Offen
#43,239 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bug Domain: This-Typing
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit dem verlinkten TypeScript Playground und dem Beispiel callback.bind(ctx) und vergleiche es mit dem explizit typisierten .bind-Aufruf. Verfolge, wie der this-Typ, der generische Funktionsattribute enthält, für Function.bind inferiert wird. Die Aufgabe ist abgeschlossen, wenn der inferierte Kontext CallbackCtx<string> aus ctx verwendet und der nicht annotierte Aufruf keinen Fehler erzeugt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.