Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functions with fewer parameters NOT assignable to functions with more parameters defined as tuples union #56766

Open
AntonNevsgodov opened this issue Dec 13, 2023 · 2 comments Β· May be fixed by #49218
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@AntonNevsgodov
Copy link

AntonNevsgodov commented Dec 13, 2023

πŸ”Ž Search Terms

"tuple union" "tuple spread"

πŸ•— Version & Regression Information

This is the behavior in every version I tried, and I reviewed the FAQ for entries about

⏯ Playground Link

Playground Link

πŸ’» Code

type Numbers = [a: number, b: number];
type Strings = [a: string, b: string];

type TupleArgs = (...args: Numbers) => void;
const f1: TupleArgs = (a) => console.log(a); // πŸ‘Œ

type SignatureOverload = { (...args: Numbers): void; (...args: Strings): void; }
type FnsIntersection = ((...args: Numbers) => void) & ((...args: Strings) => void);
type TuplesUnionArgs = (...args: Numbers | Strings) => void;

const f2: SignatureOverload = (a) => console.log(a); // πŸ‘Œ
const f3: FnsIntersection = (a) => console.log(a); // πŸ‘Œ
const f4: TuplesUnionArgs = (a) => console.log(a); // ❌

/**
 * Type '(a: string | number) => void' is not assignable to type 'TuplesUnionArgs'.
 *   Types of parameters 'a' and 'args' are incompatible.
 *     Type 'Numbers | Strings' is not assignable to type '[a: string | number]'.
 *       Type 'Numbers' is not assignable to type '[a: string | number]'.
 *         Source has 2 element(s) but target allows only 1.(2322)
 */

πŸ™ Actual behavior

Type '(a: string | number) => void' is not assignable to type 'TuplesUnionArgs'.
  Types of parameters 'a' and 'args' are incompatible.
    Type 'Numbers | Strings' is not assignable to type '[a: string | number]'.
      Type 'Numbers' is not assignable to type '[a: string | number]'.
        Source has 2 element(s) but target allows only 1. (2322)

πŸ™‚ Expected behavior

(a: string | number) => void should be assignable to (...args: [a: number, b: number] | [a: string, b: string]) => void

Additional information about the issue

Since this issue has 2 pretty clean workarounds (signature overload, functions intersection), it is not critical.
But it is an instance of inconsistent behaviour (which is annoying).

@AntonNevsgodov
Copy link
Author

AntonNevsgodov commented Dec 13, 2023

βœ… Workarounds:

Use signature overloading

type Fn = {
  (...args: [a: number, b: number]): void;
  (...args: [a: string, b: string]): void;
};
const fn: Fn = (a) => console.log(a); // πŸ‘Œ

Use functions intersection

type Fn = ((...args: [a: number, b: number]) => void) & ((...args: [a: string, b: string]) => void);
const fn: Fn = (a) => console.log(a); // πŸ‘Œ

Rest + destruct arguments

This workaround is not convenient to use, but it exists

type Fn = (...args: [a: number, b: number] | [a: string, b: string]) => void;
const fn: Fn = (...[a]) => console.log(a); // πŸ‘Œ

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Help Wanted You can do this labels Dec 13, 2023
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Dec 13, 2023
@Andarist
Copy link
Contributor

Andarist commented Jan 6, 2024

Duplicate of #48663

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants