-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
[Suggestion] Compiler Flag to treat types as immutable #16317
Comments
what about all the other types? |
@mhegazy good question! Primitives
However, I have discovered that I can mess things up pretty good by either (1) shadowing the prototype method such as // Shadow the valueOf method
const b = new Boolean(true);
b.valueOf = () => false;
console.log(b.valueOf()); // false
// Reassign the startsWith method
String.prototype.startsWith = () => true;
const s = 'hello';
console.log(s.startsWith('watermelon')); // true So perhaps we need to define the following interfaces: Built-in Objects For me it would be enough to have readonly versions of the following (just going down the list in MDN):
I know it would be a bit tedious to go through and create readonly interfaces for all of those objects, but it's not impossible. I could probably do that and submit a pull request. The real work would be adding the flag to cause the compiler to infer it. Motivation |
@mhegazy I was thinking about this some more. Honestly, I don't think we need to worry about making the |
I think a better approach is to consider all types immutable by default; mutable types should be marked as such. related to #10725.
|
TypeScript Version: 2.3.4
Code
I'm aware of #10725, but I want to take it one step further and add a compiler flag (e.g.
--immutable
) that will cause the compiler to inferReadonly
,ReadonlyArray
,ReadonlyMap
, andReadonlySet
(and any other data structures I've missed) everywhere. The flag will also force you to useconst
. Thus eliminating mutability from the language.The text was updated successfully, but these errors were encountered: