microsoft / microsoft/TypeScript
strictBindCallApply error mentions `this` type mismatch, but elaboration points to parameters, even when `this` type matches
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
🔎 Search Terms
strictBindCallApply this arg error
🕗 Version & Regression Information
- This changed between versions 4.4 and 4.5
⏯ Playground Link
💻 Code
export type CallbackA = (arg1: string, arg2: number) => number;
export type CallbackB = (arg1: string) => number;
interface Functions {
a: (arg1: string, callback: CallbackA) => number;
b: (arg1: string, callback: CallbackB) => number;
}
declare const functions: Functions;
export function create<T extends keyof Functions>(key: T): Functions[T] {
return (arg1: string, cb: CallbackA | CallbackA) => functions[key].call(undefined, arg1, cb)
}
declare let anyFunc: Functions[keyof Functions]
declare const cb: CallbackA | CallbackA;
anyFunc.call(undefined, "", cb)
🙁 Actual behavior
The error message says:
The 'this' context of type '((arg1: string, callback: CallbackA) => number) | ((arg1: string, callback: CallbackB) => number)' is not assignable to method's 'this' of type '(this: undefined, args_0: string, args_1: CallbackA) => number'.
Type '(arg1: string, callback: CallbackB) => number' is not assignable to type '(this: undefined, args_0: string, args_1: CallbackA) => number'.
Types of parameters 'callback' and 'args_1' are incompatible.
Type 'CallbackA' is not assignable to type 'CallbackB'.
Target signature provides too few arguments. Expected 2 or more, but got 1.(2684)
Or:
The 'this' context of type '((arg1: string, callback: CallbackA) => number) | ((arg1: string, callback: CallbackB) => number)' is not assignable to method's 'this' of type '(this: undefined, args_0: string, args_1: CallbackA) => number'.
Type '(arg1: string, callback: CallbackB) => number' is not assignable to type '(this: undefined, args_0: string, args_1: CallbackA) => number'.(2684)
In neither example, this is not the problem.
In fact, if you add this: void to interface Functions and then cast to void, you get this:
The 'this' context of type '((this: void, arg1: string, callback: CallbackA) => number) | ((this: void, arg1: string, callback: CallbackB) => number)' is not assignable to method's 'this' of type '(this: void, args_0: string, args_1: CallbackA) => number'.
Type '(this: void, arg1: string, callback: CallbackB) => number' is not assignable to type '(this: void, args_0: string, args_1: CallbackA) => number'.
Types of parameters 'callback' and 'args_1' are incompatible.
Type 'CallbackA' is not assignable to type 'CallbackB'.(2684)
Or even undefined:
The 'this' context of type '((this: undefined, arg1: string, callback: CallbackA) => number) | ((this: undefined, arg1: string, callback: CallbackB) => number)' is not assignable to method's 'this' of type '(this: undefined, args_0: string, args_1: CallbackA) => number'.
Type '(this: undefined, arg1: string, callback: CallbackB) => number' is not assignable to type '(this: undefined, args_0: string, args_1: CallbackA) => number'.(2684)
None of these makes sense.
🙂 Expected behavior
The message doesn't claim that this is the problem; the problem is in other params.
If you go back to 4.4, you get a more reasonable error that simply complains about the callback param:
Argument of type 'CallbackA' is not assignable to parameter of type 'CallbackB'.(2345)
Additional information about the issue
Noticed this while attempting to enable strictBindCallApply on our repo.
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
Riproduci la diagnostica strictBindCallApply dal TypeScript Playground collegato, confrontando le versioni 4.4 e 4.5. Traccia la diagnostica per le chiamate che coinvolgono Functions[keyof Functions] e verifica che il messaggio finale identifichi il parametro di callback incompatibile invece di incolpare il contesto this.
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
- Specificata chiaramente
- Idoneità per principianti
- 45/100