microsoft / microsoft/TypeScript

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

Open
#52,305 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: check: Type Inference Help Wanted
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
1d 19h
Merged PRs (30d)
117

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the example in the TypeScript playground using version 3.3.3 and a later version, then compare the inferred type of the second argument in the two calls. Done means the inline call accepts 'label' consistently with the non-inlined call without requiring the explicit generic type argument.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.