Microsoft/TypeScript

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

Open

#52.305 aperta il 19 gen 2023

Vedi su GitHub
 (3 commenti) (0 reazioni) (0 assegnatari)TypeScript (6726 fork)batch import
BugDomain: check: Type InferenceHelp Wanted

Metriche repository

Star
 (48.455 star)
Metriche merge PR
 (Merge medio 6g 17h) (9 PR mergiate in 30 g)

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 contributor