microsoft / microsoft/TypeScript

Equivalent arguments typed with tuples aren't assignable to the same functions with overloads when the same function is recursively referenced

Open
#45,466 7 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

Bug Report

🔎 Search Terms

arguments, overloads, function, tuple

🕗 Version & Regression Information

All versions with support for tuple arguments (upto v4.5.0-dev.20210815).

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about function overloading (there is nothing in the FAQ about tuples)
⏯ Playground Link

Playground link with relevant code

💻 Code
type START = 0
type DATA = 1

type Args<In, Out> =
  | [t: START, other: FnWithArgs<Out, In>]
  | [t: DATA]

type FnWithArgs<In, Out> = (...args: Args<In, Out>) => void

interface FnWithOverloads<In, Out> {
  (t: START, other: FnWithOverloads<Out, In>): void;
  (t: DATA): void;
}

declare const withArgs: FnWithArgs<1, 2>

// these should be assignable to one another, but aren't:
const assertion: FnWithOverloads<1, 2> = withArgs
🙁 Actual behavior

Two types that should be functionally equivalent aren't assignable to one another:

Type 'FnWithArgs<1, 2>' is not assignable to type 'FnWithOverloads<1, 2>'.
  Types of parameters 'args' and 't' are incompatible.
    Type '[t: 0, other: FnWithOverloads<2, 1>]' is not assignable to type 'Args<1, 2>'.
      Type '[t: 0, other: FnWithOverloads<2, 1>]' is not assignable to type '[t: 0, other: FnWithArgs<2, 1>]'.
        Type at position 1 in source is not compatible with type at position 1 in target.
          Type 'FnWithOverloads<2, 1>' is not assignable to type 'FnWithArgs<2, 1>'.
            Types of parameters 't' and 'args' are incompatible.
              Type 'Args<2, 1>' is not assignable to type '[t: 0, other: FnWithOverloads<1, 2>]'.
                Type '[t: 1]' is not assignable to type '[t: 0, other: FnWithOverloads<1, 2>]'.
                  Source has 1 element(s) but target requires 2.

I've tried simplifying the example, and this issue doesn't seem to occur when the parameters of the function don't reference themselves in their signature.

🙂 Expected behavior

No error, types should be assignable to one another.

Related issues

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 TypeScript Playground reproduction linked in the issue and inspect the recursive assignability behavior between tuple-argument functions and overloaded functions. The work is done when the shown assignment succeeds without an error while preserving correct checking for the recursive signatures.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.