Skip to content

Tuple Restriction Issue: Inconsistent Behavior with Array Methods #58701

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

Closed
srosama opened this issue May 29, 2024 · 2 comments
Closed

Tuple Restriction Issue: Inconsistent Behavior with Array Methods #58701

srosama opened this issue May 29, 2024 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@srosama
Copy link

srosama commented May 29, 2024

🔎 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.
@MartinJohns
Copy link
Contributor

Duplicate of #40316.

@ahejlsberg ahejlsberg added the Duplicate An existing issue was already created label May 29, 2024
@typescript-bot
Copy link
Collaborator

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

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants