microsoft / microsoft/TypeScript

Overload causes generic function to loose type specificity

Aperta
#52,381 1 commento 2 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Needs Investigation
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

Bug Report

🔎 Search Terms

generic overloaded function type narrowing

🕗 Version & Regression Information
  • This is the behavior in every version I tried (4.8, 4.9)
⏯ Playground Link

https://www.typescriptlang.org/play?#code/FASwdgLgpgTgZgQwMZQAQEFUG9itQgLlQEYBuYAX2FElkRVQCFUoAPaMAEwGcNtdUAIyIAmclRrR4yNAHUYCAA6LYxADwAVFuyhde6AHz88ANwQAbAK5QiG8dXBT6chctgjN2jjwxGcpi2tbe2A4SzAkCBAAezBUcIB3V082b30DAAokpRVOInkc1U0DAEpbUPDImLjE5K1U3R9DLNdc-Nb3YrLUDQqIqNj4sGzFFJ09XxbCvNQCtxh1DSMAH1mOmA8l7q1-VBgoCEsYOJHcgDozKygQpFjuCCEiZgBebHwiYgAaR9QRVApyLcwPdUKcoDM5ioFmpGEZXlhUJcgkJ-tQgSDoPdiKhXrUlFM3JwSqRUAB6UmoTEQYhPVAZW4wfaRErAdEPKl-V4ABRg0QAtiBuFAzvtuNFzCYoATciUzhAABa6DJ4xTEskUjlEHn8wVQNSGOnceXRSzmThCNDagVCmGlVl3dlQe4AZhxqAA2mDOABdM58-EqtXkylOiDOojod3ew3G03mwRoRhRllAA

💻 Code

interface A {
  a: 1;
}

interface B extends A {
  b: 2;
}

interface Wrapper1<T extends A> {
  value: T;
}

interface Wrapper2<T extends A> {
  value: T;
}

function unwrap<T extends A>(wrapped: Wrapper1<T>): T
function unwrap<T extends A>(wrapped: Wrapper2<T>): T // Comment out this line, and types will become inferred correctly
function unwrap<T extends A>(wrapped: Wrapper1<T> | Wrapper2<T>): T {
  return wrapped.value;
}

const b: B = { a: 1, b: 2 };
const wrapped: Wrapper1<B> = { value: b }

const test1 = unwrap(wrapped); // test1: B (correct)
const test2 = Promise.resolve(wrapped).then(unwrap); // test2: Promise<A> (should be Promise<B>)
const test3 = [wrapped].map(unwrap); // test3: A[] (should be B[])
🙁 Actual behavior

In test2 and test3, the generic type of the overloaded function unwrap looses it's specificity. The types can be narrowed to Promise<B> and B[] respectively, but actually it is inferred as Promise<A> and A[] respectively. Removing the one of the overloads of the function unwrap (so it is no longer overloaded) will resolve the problem.

🙂 Expected behavior

As explained above, the inferred types of test2 and test3 should be inferred as Promise<B> and B[] respectively.

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 la riproduzione collegata in TypeScript Playground e confronta l’inferenza per test1, test2 e test3 quando sono presenti entrambi gli overload. Traccia il comportamento dell’overload generico e dell’inferenza dei tipi; il lavoro è completato quando test2 è Promise e test3 è B[] senza rimuovere alcun overload.

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.