microsoft / microsoft/TypeScript

Unable to resolve generic type when using an overloaded function as parameter

Aperta
#52,305 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Bug Domain: check: Type Inference Help Wanted
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

Bug Report

🔎 Search Terms

overload parameter function

🕗 Version & Regression Information

Using the playground, the code works when I choose version 3.3.3. After that version, the problem comes up.

💻 Code
type SpecialType<T> = () => void & { value: T };

function specialFunction<T>(): SpecialType<T>;
function specialFunction<T>(source: Record<string, T>): Record<string, T>;
function specialFunction<T>(source?: Record<string, T>): SpecialType<T> | Record<string, T> {
  return source as any;
}

const otherFunction = <K extends keyof T, T>(source: Record<string, T>, property: K): Record<string, T[K]> => ({ source, property }) as any;

const obj: Record<string, { label: string }> = { value: { label: 'hello world' } };

const defined = specialFunction(obj);
const working = otherFunction(defined, 'label');

const nonworking = otherFunction(specialFunction(obj), 'label');

const alsoworking = otherFunction(specialFunction<{ label: string }>(obj), 'label'); 
🙁 Actual behavior

The non-inlined call chain works and label is recognized as a property name. The inlined call as a parameter states, that the second parameter type is never(instead of keyof T).

🙂 Expected behavior

No different type resolution regardless of the call type.

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

Riproduci l'esempio nel TypeScript playground usando la versione 3.3.3 e una versione successiva, quindi confronta il tipo inferito del secondo argomento nelle due chiamate. Il lavoro è completato quando la chiamata inline accetta 'label' in modo coerente con la chiamata non inline, senza richiedere l'argomento di tipo generico esplicito.

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.