microsoft / microsoft/TypeScript

Generics; ReturnType<Foo> != ReturnType<typeof foo>

Aperta
#24,277 7 commenti 1 reazione 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
2g 4h
PR unite (30g)
132

Descrizione

TypeScript Version: Version 2.9.0-dev.20180516

Search Terms: generic function returntype

I couldn't think of a good title for this,

Code

type Delegate<T> = () => T;

function executeGeneric<
    X,
    DelegateT extends Delegate<X> 
>(delegate: DelegateT): ReturnType<DelegateT> {
    //Type 'X' is not assignable to type 'ReturnType<DelegateT>'.
    const x: ReturnType<typeof delegate> = delegate();
    //Type 'X' is not assignable to type 'ReturnType<DelegateT>'.
    const y: ReturnType<DelegateT> = delegate();
    //Type 'X' is not assignable to type 'ReturnType<DelegateT>'.
    return delegate();
}

Expected behavior:

Return successfully.

Intuitively, to me,

  1. delegate is of typeDelegateT.
  2. ReturnType<DelegateT> and ReturnType<typeof delegate> should be the same

However,

Actual behavior:

Type 'X' is not assignable to type 'ReturnType<DelegateT>'.

Playground Link: Here

Related Issues:

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 dalla riproduzione collegata di TypeScript Playground e confronta i tipi di ReturnType e ReturnType nella funzione generica. Determina se gli errori di assegnazione segnalati debbano essere eliminati mantenendo la relazione generica indicata, quindi verifica che la riproduzione non segnali più tali 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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.