microsoft / microsoft/TypeScript

Inference from annotated parameters of context-sensitive functions is order dependent

Open
#56,459 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

🔎 Search Terms

inference annotated parameters context sensitive order

🕗 Version & Regression Information
  • This is the behavior in every version I tried
⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.4.0-dev.20231118#code/CYUwxgNghgTiAEAzArgOzAFwJYHtXwxAGcMAeAFQBp4BBagIQD4AKHAIwCsAueAbwCh48TAA8e5ANyD4UHs1m1qo8QEp4AXkbwAbjizApQtnOPx6SjGPhUZPGms069B-gF8VPXfqn9CJZgJCyvAA5Ig4OCGU0gryPCQwWKgA5hYiDlq8rtFGJvEYiSlp1FAZfNluKj5+GAHSwWERUdKmzKYJSanCliVlWTm28HHwHUXd6RqZFe5SQA

💻 Code
declare function test<T, A, B>(obj: {
  ctx: T;
  a: (a: A, ctx: T) => void;
  b: (b: B, ctx: T, a: A) => void;
}): void;

test({
  ctx: 'foo',
  a: (a: string, ctx) => {},
  b: (b: string, ctx, a) => {},
});

test({
  ctx: 'foo',
  b: (b: string, ctx, a) => {},
  a: (a: string, ctx) => {},
});
🙁 Actual behavior

The second call doesn't infer properly

🙂 Expected behavior

I'd expect the second call to be inferred as good as the first one

Additional information about the issue

related to https://github.com/microsoft/TypeScript/issues/47599

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 two calls in the linked TypeScript Playground and compare their inference behavior, then read the related issue #47599 for context. Investigate the type inference and contextual typing paths involved; done means the second object-property order infers as successfully as the first without regressing the existing example.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.