Skip to content

Tuple Restriction Issue: Inconsistent Behavior with Array Methods聽#58701

Closed as not planned

Description

@srosama

馃攷 Search Terms

In TypeScript, tuples are expected to have a fixed length and type for each element. However, using array methods like push allows additional elements to be added, which violates the tuple's intended constraints.

馃捇 Code

let user: [number, string] = [1, "Ahmed"];

// Adding elements beyond the defined tuple length
user.push(2);
user.push(3);

馃檨 Actual behavior

The current behavior allows additional elements to be pushed into the tuple without any compile-time errors, leading to a runtime output of:
1
Ahmed
2
3

馃檪 Expected behavior

The TypeScript compiler should raise an error when attempting to add elements beyond the fixed length of the tuple. For example:
let user: [number, string] = [1, "Ahmed"];
user.push(2); // Should raise a compile-time error
user.push(3); // Should raise a compile-time error

Additional information about the issue

Suggestions

Compile-Time Checks: Introduce stricter compile-time checks for tuple operations to prevent methods like push, pop, shift, and unshift from modifying the tuple length.
Utility Types: Provide utility types or methods to safely handle tuple operations without violating their constraints.
Documentation: Enhance the documentation to clarify the behavior of tuples and the limitations of using array methods on them.

Activity

  1. MartinJohns commented on May 29, 2024

    @MartinJohns
    Contributor

    Duplicate of #40316.

  2. typescript-bot commented on Jun 1, 2024

    @typescript-bot
    Contributor

    This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

  3. locked as resolved and limited conversation to collaborators on Oct 22, 2025
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

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions