Microsoft/TypeScript

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

Open

#52,305 opened on Jan 19, 2023

View on GitHub
 (3 comments) (0 reactions) (0 assignees)TypeScript (48,455 stars) (6,726 forks)batch import
BugDomain: check: Type InferenceHelp Wanted

Description

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