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

Tuple should be readonly by default #40316

Open
Razdva122 opened this issue Aug 29, 2020 · 2 comments
Open

Tuple should be readonly by default #40316

Razdva122 opened this issue Aug 29, 2020 · 2 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@Razdva122
Copy link

Any tuples can mutate, this entails incorrect typing

TypeScript Version: 4.1.0-dev.20200828

Search Terms:
"readonly tuple"
"immutable tuple"

Code

const exampleSort: [number, string] = [2, '1'];

exampleSort.sort();

exampleSort // Type: [number, string], RealType: [string, number]

const exampleSplice: [number, string] = [2, '1'];

exampleSplice.splice(0, 1);

exampleSplice // Type: [number, string], RealType: [string]

Expected behavior:

const exampleSort: [number, string] = [2, '1'];

// Property 'sort' does not exist on type '[number, string]'.(2339)
exampleSort.sort();

const exampleSplice: [number, string] = [2, '1'];

// Property 'splice' does not exist on type '[number, string]'.(2339)
exampleSplice.splice(0, 1);

Actual behavior:

const exampleSort: [number, string] = [2, '1'];

exampleSort.sort();

const exampleSplice: [number, string] = [2, '1'];

exampleSplice.splice(0, 1);

Playground Link: https://www.typescriptlang.org/play?ts=4.1.0-dev.20200828#code/MYewdgzgLgBApgDwIYFsAOAbOBlEAnKALhgG0wBXFAIzjwBoZo8BLMAcwF0YBeUgJgYByAIyCOAbgBQkxKkw58UAHQRFACgCUUmcnRZcBGAHojMACoBPNHGJlKNeoygt2HBgCU4SDJeu2mrGwMFNS0HNKgkLCyejiYzMA2pCEODAGuPPxCohLSMfLY8YkqRXBqAAwMwlp5ugWlxqa+SXahjumcHl4+Vi0d4dKSQA

Related Issues:

@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Sep 9, 2020
@jcalz
Copy link
Contributor

jcalz commented Aug 7, 2021

Note that something like this was previously declined in #6325.

I don't think it should be readonly since you should be able to write into it the way you would with any object:

const x: [number, string] = [1, "hello"];
x[0]++; // should be fine
x[1] += " there"; // should be fine

Instead, you'd probably just want to omit methods that can violate tuple constraints like "pop" | "push" | "reverse" | "shift" | "sort" | "splice" | "unshift" | "fill" | "copyWithin".

(Aside: it's fun to think about which tuple types could still allow which methods safely... perhaps [string, string, string] could still allow sort() and [number, string, number] could still allow reverse() and [string, ...string[]] could still allow push() and unshift().)


But I imagine that any form of this would be a big breaking change, right? Lots of code has Foo[] in places where readonly Foo[] would be fine, and having tuples suddenly not assignable to array types would mess that up.

@RyanCavanaugh
Copy link
Member

Merging in additional issue: This should apply to all mutating methods (splice, shift, unshift, etc)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants