Microsoft/TypeScript

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

Open

#52.305 geöffnet am 19. Jan. 2023

Auf GitHub ansehen
 (3 Kommentare) (0 Reaktionen) (0 zugewiesene Personen)TypeScript (6.726 Forks)batch import
BugDomain: check: Type InferenceHelp Wanted

Repository-Metriken

Stars
 (48.455 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 6T 17h) (9 gemergte PRs in 30 T)

Beschreibung

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.

Contributor Guide