-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[prefer-readonly-parameter-types] FR: Add an option to allow Readonly<>
to satisfy the rule
#4061
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
Comments
I'm not sure I understand how this is a valid trade-off as this is completely at-odds with what the rule enforces. With this option it means that very simple cases can leak through and cause side-effects in your code. Eg function foo(arg: Readonly<{prop: {other: string}}>): void {
arg.prop.other = 'oops';
} There are utility types that exist and that allow you to make types deeply readonly such as the one published to npm in this utility package - so I'm not sure why it's better to add a large hole to the rule instead of using one of those types. |
Hi, Thanks for the link to the $( '.some-class' ).each( ( _, container: HTMLElement ) => {
// Some code that only reads from container
} ); When I make It seems to me that in this case, I have two options - either settle for |
Another option is to consider using the
The middle ground here is that you can turn the rule on or off. That's how you can start with this rule. We do not accept options that help people adopt rules because options live forever. They're not temporary like a codebase transition. Which means that people can forget to turn these "transitional" options off. False positives do suck - but are "okay" because they can be seen, understood, and ignored (disabled via comment). False positives annoy users by being annoying. False negatives irrevocably damage a user's trust in linting. Your suggestion of a blanket option that just allows single-level readonly is too far-reaching and will very, very easily lead to false negatives (like I have shown). The above two reasons are enough for me to reject your initial proposal. Given your example, however, now I can actually understand your problem. For reference - this is why our templates all ask for example code when filing issues. Code is worth 1000 words and it can get everyone on the same page much faster than just a description - which can avoids a lot of back-and-forth. Making a type is deeply readonly - as I've indicated. The issue is that there are certain types that just don't play nicely with deep readonly-ness. You don't actually want an option that allows just single-level readonly. You want an option that allows you to ignore certain impossible-to-deep-readonly types. I'm more than happy to accept a change which adds an option to ignore types that are a subtype of |
I'm not sure you and I understand the concept of middle ground the same 🤷
I think that is a very good point, at the same time, a rule with very many FPs is a rule that isn't going to be enabled at all. I am currently trying to "fix up" a project to use readonly parameters, however, in something like a third of the cases I have to disable the rule. I would go by "Don't let perfect be the enemy of good" here. I think we have different mental frameworks here - you are thinking weaker rule vs. stronger rule, I am thinking weaker rule vs. no rule.
Is there a way to do this locally? I don't know of any :/ That would be much better though... You are right that certain types don't play nicely with deep readonlyness. It's not only |
You need to make a distinction between "types that need deep Readonly instead of single-level Readonly" and "types that are heavy, deep and cyclic and cannot work with deep Readonly" The former there are many, and the latter there are few. The former is solved easily by a type like the one that I linked, and the latter cannot be solved by anything. The latter is all that we need to solve for.
Your concept of middle ground is "turn it on weakly across the entire project at once and maybe then gradually improve the types". My concept of middle ground is "turn it on strictly on a subset of folders and gradually expand that to cover the entire codebase". Taking your example of migrating a codebase from JS to TS. Would you do a single commit which turns every single file to a We use scoped/targeted linting a lot at Facebook. We enforce certain folders strictly adhere to linting practices and we treat other folders as weakly linted. |
Oh, in the part at the end of my last comment (about the ~ third of types that tuned out to be problematic) I was only talking about those that cannot be solved by
Honestly that is exactly what I did :D But I get your point, I am not using TS on big repositories with multiple people actively involved. But it seems to me that we have reached as far as we could - I'd rather have a weaker rule that doesn't have to be manually turned off in a third of the cases (YMMV of course), you prefer a stricter rule that protects the user better in the cases where it is applicable - I think that is a difference of opinion and I respect that you as a maintainer have the final say in such a case. Is there any other way to make the rule easier to use in general? You mentioned adding an exception specifically for |
We can create an allowlist of types that are problematic and expand on them as required.
For example here is some code we could generalise from an existing rule: typescript-eslint/packages/eslint-plugin/src/rules/no-implied-eval.ts Lines 83 to 91 in 71c9370
If you've got some examples of the sorts of types that are problematic, we can discuss a solution for them. If there "custom" types or such - then I'd like to gain an understanding of what these types are so we can figure out the best approach for them. |
Thanks, I opened that as a new issue to keep the record untangled. |
Uh oh!
There was an error while loading. Please reload this page.
Repro
See #2699 for an example of when this could be useful.
Rationale
I see this as a trade-off - turning such an option on would reduce the strictness of the rule (as far as I understand it,
Readonly<>
only guarantees shallow readonlyness, this rule checks for deep readonlyness), however, it would make it much easier to use in certain cases like #2699 or #2079.The text was updated successfully, but these errors were encountered: