microsoft / microsoft/TypeScript

Nested inference fails if function is called inline

Open
#54,184 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Domain: check: Type Inference Possible Improvement
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

Bug Report

🔎 Search Terms

Nested inference, function call

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about nested function call inference
⏯ Playground Link

With libraries
Minimal reproduction

💻 Code
type NoInfer<T> = [T][T extends any ? 0 : never]

type ErrorFn = (error: unknown) => void

declare const genericFn: <T>(args: {
  parser: (p: unknown, errorFn: ErrorFn) => T
  handler: (data: { body: NoInfer<T> }) => unknown
}) => T

declare const createParser: <T>(arg: T) => (p: unknown, errorFn: ErrorFn) => NoInfer<T>;

genericFn({
  parser: createParser(1 as const),
  handler: ({ body: _ }) => {
    //         ^?
    // unknown, should be 1
  }
})
🙁 Actual behavior

Handler body is not inferred, becomes default of unknown.

🙂 Expected behavior

Handler body should be inferred from parser, and be 1.

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

Start with the linked minimal reproduction in the TypeScript Playground, then compare it with the version using libraries. Trace how the inline createParser call contributes to generic inference and verify the handler's body type. Done means the reproduced case infers body as 1 rather than unknown.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.