microsoft / microsoft/TypeScript
Shorter tuple inference gets picked over longer inference when both come from rests
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.4k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
🔎 Search Terms
inference contravariant covariance rest shorter longer tuples
🕗 Version & Regression Information
- This is the behavior in every version I tried
⏯ Playground Link
💻 Code
declare function fn<A extends readonly unknown[]>(
arg: (...args: A) => void,
...args: A
): A;
const inferred = fn((a: number) => {}, 1, 2); // Expected 2 arguments, but got 3.(2554)
// ^? const inferred: [a: number]
const inferred2 = fn((a: number, b?: number) => {}, 1); // ok
// ^? const inferred2: [number]
const inferred3 = fn((a: number, b?: number) => {}, 1 as const); // ok
// ^? const inferred3: [1]
🙁 Actual behavior
inferred is [a: number] and the function call fails
🙂 Expected behavior
inferred should be [number, number] and the function call should succeed.
Additional information about the issue
Based on inferred2 and inferred3 we can see that the covariant inference is preferred in a situation like this. The problem is the .length mismatch between those 2 candidates but since the contravariant candidate comes from a rest position it should be ignored when comparing those 2 candidates and the covariant inference should be preferred
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked TypeScript Playground example and reproduce the inferred tuple types and argument-count diagnostic. Trace tuple inference for the contravariant function parameter and rest arguments; done means the example infers [number, number] for inferred and accepts all three arguments without changing the existing inferred2 and inferred3 behavior.
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