microsoft / microsoft/TypeScript
Tuple should be readonly by default
Open
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);
Related Issues:
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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