microsoft / microsoft/TypeScript

Type inference for `Function.call` return type on generic functions

Aperta
#54,319 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

🔎 Search Terms

type infer function call generic

⏯ Playground Link

Playground link with relevant code

💻 Code
function numberEcho(x: number): number {
    return x;
}

function genericEcho<T>(x: T): T {
    return x;
}

// This works
const resultNumber: number = numberEcho.call(this, 1);

// Type Error: Type 'unknown' is not assignable to type 'string'.(2322)
const resultString: string = genericEcho.call(this, 'hello');
🙁 Actual behavior

Type inference of genericEcho.call return type failed even when all the argument types are known at call site.

Of course explicitly hinting the return type would work but it's not good:

const resultString: string = genericEcho.call<typeof this, [string], string>(this, 'hello');
🙂 Expected behavior

I would expect type inference of generic function return type to work if all arguments types are known.

I think #40179 is related but the example I provided here is much more common since it exists in standard library.

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 confronta i tipi restituiti inferiti per gli esempi diretto e generico di Function.call. Il lavoro è completato quando la chiamata generica inferisce il tipo dell'argomento e accetta il risultato come stringa, mantenendo il comportamento esistente coperto da un controllo di regressione appropriato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
30/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.