-
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
Explicit documentation of mutable function parameters #9741
Comments
It’s difficult (if not impossible?) to detect mutation though. Think about this: function doSomething(user: User) {
user.updateSomething();
} Here, |
@poke Classes are tricky for sure. There could be a way to mark that method as mutating the class in the definition of class User {
mutable updateSomething() { ... }
} Unless TypeScript could infer that the In other words: I think this is analogous to optional typing. If you pass an immutable parameter to a function which mutates it, that's unfortunate. But you're no worse off than in plain JS. But if TS knows that it will mutate it, it can warn you. And that's tremendously helpful! |
We have discussed in the past supporting |
@mhegazy were there any conclusions? Having a way to make parameters const-by-default would make this a more useful check, but it would be a more radical change. |
I do not think making arguments immutable by default is an option. You can always have a lint rule to make all parameters immutable if you so chose. |
Related/context: #9998 |
Bumping this issue as making argument explicitly |
We're investigating behavior like this as part of a larger effort. See #17502 for some preliminary notes, but closing this one for housekeeping purposes since the overall |
@mhegazy What is the status on that discussion? Is there an issue being tracked? I was going to open a new issue till I searched and found this topic. The link mentioned above covers mostly immutable/mutable stuff but not shallow immutable like const. I really want something in the lines of this:
|
JavaScript makes it easy to mutate Object parameters:
Mutating an argument is somewhat unusual, so mutable parameters are typically documented in comments. But it would be better if this documentation were part of the function declaration, ala
const
in C++. That way inadvertent mutations could be caught withtsc
.For example:
To preserve compatibility, function parameters would all be
mutable
by default unless a newstrictMutableChecks
flag were set.Scalar parameters could always be modified, since these changes wouldn't be visible outside the function:
There would have to be some way for declaration files to indicate whether they were explicitly documenting mutable parameters or using the standard mutable-by-default.
It would also be nice if there were a way to mark constants as immutable. This could help catch whole classes of bugs, e.g. forgetting that
_.extend
mutates its first parameter in addition to returning a combined Object:This is related to #7770 but is less ambitious, since it would only apply to function parameters.
It may be related to #8575 but there's not much context there.
The text was updated successfully, but these errors were encountered: