Skip to content

Fails to calculate arguments length, when combining tuple spread with extra arguments #29248

Description

TypeScript Version: 3.3.0-dev.20190103

Search Terms: tuple, array, spread, arguments, length

Code

const three: [number, number, number] = [1, 2, 3];
const two: [number, number] = [1, 2];
const one: [number] = [1]; 

const foo = (a: number, b: number, c: number) => undefined;

// EXPECTED: correctly realizes there are three args
foo(...three);

// EXPECTED: correctly realizes there are two args
// error: Expected 3 arguments, but got 2.
foo(...two);

// EXPECTED: correctly realizes there is one arg
// error: Expected 3 arguments, but got 1.
foo(...one);

// UNEXPECTED: does not realize there are three args
// error: Expected 3 arguments, but got 1 or more.
foo(...two, 2);

// UNEXPECTED: does not realize there are three args
// error: Expected 3 arguments, but got 1 or more.
foo(...one, 2, 3);

Expected behavior:
Correctly calculate the count of the arguments passed into foo. Add the additional arguments to the length of the tuple. Basically the last two examples should not throw an error.

Actual behavior:
When adding additional arguments after the spreaded tuple, TS fails to calculate the total amount of arguments end expects the amount of the additional arguments "+ more".

Playground Link: Link

Related Issues:

Activity

  1. ahejlsberg commented on Jan 6, 2019

    @ahejlsberg
    Member

    I'm changing this to a suggestion as the current behavior is actually intended. From the description in #24897:

    When a function call includes a spread expression of a tuple type as the last argument, the spread expression corresponds to a sequence of discrete arguments of the tuple element types.

    A spread expression in any position but the last is always processed as a spread of an array type, i.e. we consider it to contribute an unknown number of arguments.

    A key issue with this suggestion is whether we're willing to trust that a fixed-length tuple type always has exactly the declared fixed length. We do the best we can to check it, but it is obviously easy to circumvent.

  2. hpohlmeyer commented on Dec 13, 2021

    @hpohlmeyer
    Author

    I was just going through my open bug reports and looked at my playground link.
    The behavior seems to have changed with TS 4.0 and variadic tuple types.

    I guess it can be closed than.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    In DiscussionNot yet reached consensusSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions