microsoft / microsoft/TypeScript

TS2769 : Function.bind

Aperta
#42,700 1 commento 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Bug Domain: This-Typing
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

Bug Report

🔎 Search Terms

function bind

🕗 Version & Regression Information

TypeScript 4.1 & 4.2-beta

⏯ Playground Link

playground

💻 Code
type Funcs = {
    first (value: string): void;
    second (value: number): void;
}

type Store = {
    commit <Name extends keyof Funcs> (
        name: Name,
        ...args: Parameters<Funcs[Name]>
    ): void;
}

type Func <Name extends keyof Funcs> = (
    ...args: Parameters<Funcs[Name]>
) => void;

declare const store: Store;

function commit<Name extends keyof Funcs> (name: Name): Func<Name> {
    // error
    return store.commit.bind(undefined, name);
}

function commit2<Name extends keyof Funcs> (name: Name): Func<Name> {
    return function (...args: Parameters<Func<Name>>) {
        // correct
        store.commit(name, ...args);
    }
}
🙁 Actual behavior
(method) commit<Name extends keyof Funcs>(name: Name, ...args: Parameters<Funcs[Name]>): void
No overload matches this call.
  Overload 1 of 6, '(this: (this: undefined, arg0: Name, ...args: [value: string] | [value: number]) => void, thisArg: undefined, arg0: Name): (...args: [value: string] | [value: number]) => void', gave the following error.
    The 'this' context of type '<Name extends keyof Funcs>(name: Name, ...args: Parameters<Funcs[Name]>) => void' is not assignable to method's 'this' of type '(this: undefined, arg0: Name, ...args: [value: string] | [value: number]) => void'.
      Types of parameters 'args' and 'args' are incompatible.
        Type '[value: string] | [value: number]' is not assignable to type 'Parameters<Funcs[Name]>'.
          Type '[value: string]' is not assignable to type 'Parameters<Funcs[Name]>'.
  Overload 2 of 6, '(this: (this: undefined, ...args: Name[]) => void, thisArg: undefined, ...args: Name[]): (...args: Name[]) => void', gave the following error.
    The 'this' context of type '<Name extends keyof Funcs>(name: Name, ...args: Parameters<Funcs[Name]>) => void' is not assignable to method's 'this' of type '(this: undefined, ...args: Name[]) => void'.
      Types of parameters 'name' and 'args' are incompatible.
        Type 'Name[]' is not assignable to type '[name: Name, ...args: Parameters<Funcs[Name]>]'.
          Target requires 2 element(s) but source may have fewer.(2769)
🙂 Expected behavior

The function commit works correctly as the function commit2. I'm a bit confused and sorry if this bug has been reported.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con il TypeScript Playground collegato e riproduci l'errore TS2769 usando l'esempio con commit generico e store.commit.bind in TypeScript 4.1 o 4.2-beta. Non viene indicato alcun file del repository né alcun test, quindi segui la gestione da parte del compilatore di Function.bind e delle tuple di parametri generiche; il lavoro è completato quando commit supera il controllo dei tipi con lo stesso comportamento di commit2.

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.