-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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: no-restricted-globals
for types
#673
Comments
I would think a better solution is to rename your helpers so that they don't clash, so that there is no weirdness, and a rule that catches non-imported vs imported isn't needed. Having developers get confused that they're using "the wrong Omit" is a pretty clear smell IMO:
|
I agree, but sometimes we don't get to decide how to name things—libraries do it for us. Examples:
In that case, we still want to catch mistakes where the global one is being used when it shouldn't. |
Side note: I really want this TS / VS Code feature to help in this case: microsoft/TypeScript#31199. |
The type provided by the library can be renamed and exported to global scope, one example. |
Hmm, that's only reasonable if you're happy polluting the global namespace. Seems a shame to me to have to sacrifice modules/imports. Also, sometimes you're dealing with not just a type but also a value, e.g. the There are different ways of managing this problem. This lint rule suggestion would enable one of them, which some people might prefer. |
Regardless of how you avoid the name conflict, we still have the core problem of "how to prevent accidental usage of the global type". Even if I name my custom |
All of the reasons you might want this for values also apply to types. So if ESLint can offer it for values, shouldn't this plugin provide parity by offering it for TS types? |
Just came across this again.
If I wanted to write this lint rule, how can I read a list of "global types" in the lint rule? |
It depends on what you mean by "global types". There are two types of global types:
For (1), it's super simple to detect them, see this example from the
For (2), it gets a bit difficult. For example, people are trying to amend the In this case, you have to default to tracing the symbol to figure out where it originated. i.e. if it was defined locally in any way (imported, type def, generic, etc), then it's not a global type. |
Thank you @bradzacher. In my specific case I only care about (1), so that's very useful 😄 |
Hey @OliverJAsh @bradzacher, I'm working on implementing @octogonz kindly helped us determine that the build process failed, because we used a type from We would like to add a |
There's no real solution right now.
There's already example code in the plugin for doing check (1): typescript-eslint/packages/eslint-plugin/src/rules/unbound-method.ts Lines 97 to 111 in 133f622
Happy to accept a PR to add this in! |
@bradzacher any examples of extension rules for reference? |
One of the simple examples is the |
Seems like this was added recently since bumping to latest caused typed returns from functions that are returning values gotten from native functions (that are returning native promises) to error. |
I needed this again (this time to ban the global I think this makes more sense as an option in How does that sound @bradzacher? If that sounds good, could I get some help in the PR? I'm a bit stuck #4441 (comment) |
When you first posted this issue back in 2019, we didn't have our own scope manager. So |
Amazing! I guess we can close this then? |
The ESLint rule
no-restricted-globals
is great, but it only applies to global values. I want the same thing, but for global types.Example usage: I want to disallow usage of the global TypeScript
Omit
type because it's not strict enough, but I still want to allow imported types of that same name to be used.This is similar to the existing
ban-types
rule, but different becauseban-types
bans the type both globally and locally.The text was updated successfully, but these errors were encountered: