-
Notifications
You must be signed in to change notification settings - Fork 607
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
[eslint-config] Proposal: Allow inferred types for local variables #2206
Comments
I'm OK with the proposal, though a fan of the |
I'm okay with this proposal as long as:
I'm personally a big fan of strict typings, but I understand why some people consider that strictness cumbersome. If this promotes adoption, I'm (somewhat begrudgingly) cool with it. |
Personally I prefer the stricter behavior of requiring types for local variables, but I could just be overly pedantic. Personal opinion aside, I think this change might be worthwhile. I was strongly opposed until I gave the inconsistency around destructuring some more thought. The main argument to always have the types declared is to avoid the need for a reader (not in an IDE that can infer and display the type, e.g. VS Code) to quickly understand the types being used. As @octogonz pointed out, we don't currently enforce this rule when destructuring which leads to having to still hunt for a function signature or type definition anyways. Consistency is probably more important than the mild inconvenience. This rule change should definitely be configurable. I think I'm ok with the default behavior being the looser option. |
Here's the PR: #2219 The |
Is this a feature or a bug?
We're converting a migrating a bunch of HBO code to use
@rushstack/eslint-config
, and in a number of cases it is awkward to have to declare types for local variables. The Rush Stack ruleset has required full type declarations for 5 years now, and lots and lots of code has been written using those rules.Good reasons to be strict
Generally declaring types for local variables greatly improves readability. Compare this:
with this:
More examples and background are explained in the comments for the config.
Reasons to be a bit more lax
But
typedef
can be awkward for certain cases such as:local data structures
algebraically composed types (that are not accessible outside their scope)
simple redundancies
This was a classic example where declarations are not always useful:
(Although I eventually realized for this specific API you can get around it like this:)
destructuring
It's also worth mentioning that the
@rushstack/eslint-config
ruleset today already makes a special exception for destructuring expressions on similar grounds:Proposal
Let's follow the model of
C#
and allow type inference for local variables, whose signatures are never seen outside of the scope where they are used.For code bases that are accustomed to requiring strict types everywhere, we can provide an "mix-in" import to restore the stricter behavior.
The motivation for explicit types is really about:
We can balance this with a couple counter goals:
typedef
is the rule people cite most when resisting our coding conventionsThis proposal arguably preserves the first two benefits, while adding the second two benefits.
@iclanton @hbo-iecheruo @KevinTCoughlin @johnguy0
The text was updated successfully, but these errors were encountered: