microsoft / microsoft/TypeScript

function members of primitives/builtins are not read-only

Open
#59,904 7 comments 1 reaction 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

🔎 Search Terms

builtin number string member readonly

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about "Common Bugs that arent Bugs"
⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.5.4&ssl=6&ssc=1&pln=1&pc=1#code/DYUwLgBAHhC8ECYDcAoKA6AbgQ2AVxAHkAzOCACgQEotcCTUVRIBPMgIgAsRhgB7dqhboAxp2wAnAIKR4wsZJmogA

💻 Code
let x = 2;
x.valueOf = (2).valueOf;

let y = "hello";
y.charAt = y.charAt;
🙁 Actual behavior

Code compiles just fine, even with --strict enabled.
Executing JavaScript fails:

can't assign to property "valueOf" on 2: not an object 
🙂 Expected behavior

Should be rejected by tsc as read-only assignment.

Additional information about the issue

Seems similar in nature to https://github.com/microsoft/TypeScript/issues/49113
Notably, array.length is allowed (because its legal in JS) but string.length is not (because it is illegal in JS). Since assigning to builtin functions is also illegal, this should be flagged by tsc.

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 with the linked TypeScript Playground example and reproduce the assignments under --strict with tsc. Trace how assignments to primitive members are checked, then add rejection for illegal writes to builtin functions while preserving legal JavaScript cases such as array.length; add or update compiler tests to cover the examples.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.