microsoft / microsoft/TypeScript
Cannot infer `thisArg` argument type of `Function.bind` for functions with a `this` type consisting of generic functions
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con il TypeScript Playground collegato e l'esempio callback.bind(ctx), confrontandolo con la chiamata a .bind tipizzata esplicitamente. Segui come viene inferito per Function.bind il tipo this che contiene proprietà di funzione generiche. Il lavoro è completato quando il contesto inferito usa CallbackCtx<string> da ctx e la chiamata non annotata non produce errori.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100