microsoft / microsoft/TypeScript

Tuple should be readonly by default

Open
#40,316 3 comments 29 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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:

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the tuple examples in the linked TypeScript Playground and compare the reported types with the runtime mutations from sort and splice. Trace the compiler's tuple and array mutability handling; done means the expected diagnostics appear for these operations without breaking valid tuple usage.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.