-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[no-redeclare] False positive for types and values with the same name #2585
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
Might want to look at similar-sounding eslint/typescript-eslint-parser#443 |
I reproduced it with 4.0.0, when the rule seems to have been added. I think previously there was only an eslint rule with the same name, and that one did not report these cases. |
This is mentioned in the rule docs:
|
Huh, didn't notice that part after reading it three times... Sorry! I'd like to ask for an option to change this behavior (e.g. allowSameNameTypeAndValue). Stuff like IO-TS (mentioned above) use this pattern a lot. Using a comment to disable the rule is not quite the same, because I would like to be notified if I do have two values or two types with the same name (I did have that issue more than once, and it's hard to notice). Also, I'd have to add a couple hundred annotations, I don't feel like having that many exceptions would be good practice. |
TBH I question why you would even bother using the rule - typescript itself does the same check much better without the need for any configuration. Why do you need to export a type that is just an alias of |
For your first question, I’m not actually using the rule myself, it comes from one of the standard presets. (Not sure which, something like prettier. I can check tomorrow if it’s important.) I assumed that since someone actually added this rule in typescript-eslint (it didn’t exist before 4.0.0) it actually does something, but I don’t really know the reasoning. (I did disable it for my project, but I’ve no idea what else it would have caught that I’ll be missing from now on.) For your second question: the code above is just a minimal example to reproduce the issue. In actual code using IO-TS the second line would be something like: type Test = t.TypeOf<typeof Test> That is much more annoying to type a few hundred times, not to mention hard to read. (The first line would also be something more complex that declares the type’s structure as an object accessible at runtime.) Also, both the variable and the type are logically representations of the same conceptual “type” (one for compile-time checks, one for run-time checks), so having the same name makes sense in the context of IO-TS: you are actually referring to the same thing, just from different perspectives. (The library tries hard to allow almost identical syntax in both contexts to help this intuition.) Having |
I added it because I know people use it. Some people don't actually use TS to build their code so they like to rely on lint rules for validation. I don't know why... it defeats the purpose of using TS - but people have weird setups 🤷♂️. If I didn't add it, then I know we would have gotten reports about the base Prior to 4.0, this was handled implicitly by our super dodgy hacked together scope analyser (it was hacked so it didn't emit multiple declarations). Post 4.0, the full scope analyser emits these as multiple declarations on purpose, so an extension rule is required to teach the rule about what these mean.
Nothing at all. var x = 1;
var x = 2; I doubt you use The full io-ts example makes much more sense, and it is definitely valid to do the shadowing there. |
OK, so if I got that right, since my current project usese TS to compile stuff, simply disabling the But you agree that in theory the usage is valid (also, overwriting standard presets is not ideal so it would be nice if the preset authors had a better option). So, should this be reopened? Should I open a separate feature request for the option? I don’t think I could do a decent job trying to add this, given how little I understand of the ts-eslint’s internals, so there’s little chance of a pull request from me anytime soon, but maybe you want to have it tracked? |
I've noticed the rule throwing an error for function overloads in typescript. |
@bradzacher I saw your comment regarding an option to allow this, and here's an use case that currently it's the best way to deal with extending enums, maybe you can reconsider that option so devs don't need to use |
I’m running into this issue when trying to upgrade. Here’s an example of the code which errors: export const Fragment = "";
export type Fragment = typeof Fragment; Maybe we can have an exception if the right hand side of the type assignment has a typeof operator? |
Introducing this Now the open question for our team is to completely disable the For such tools like |
Repro
Expected Result
I would expect the code to be allowed without complaint.
Actual Result
Additional Info
No exception was thrown. (Not clear from the issue template if you want the
--debug
output regardless, I assume it’s useful only for crashes.)The rule is enabled by some preset, not sure which. I use these: "plugin:react/recommended", "plugin:react-hooks/recommended", "standard-with-typescript", "prettier", "prettier/@typescript-eslint", "prettier/babel", "prettier/react", "prettier/standard"
The behavior changed sometimes in the last three months, it didn’t use to complain about these at the start of summer.
For the record, in my actual use case I’m using io-ts, where this is a common pattern.
Versions
@typescript-eslint/eslint-plugin
4.1.1
@typescript-eslint/parser
4.1.1
TypeScript
4.0.3
ESLint
7.9.0
node
14.7.0
The text was updated successfully, but these errors were encountered: