-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Don’t show auto-import suggestion if there’s a global by the same name #31065
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
Conversation
verify.completions({ | ||
marker: "", | ||
// The real parseInt is in 'globalVars', the fake one exported from a.ts is missing | ||
exact: ["globalThis", ...completion.globalsVars, "undefined", ...completion.statementKeywordsWithTypes], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sandersn What's going on with globalThis
here?
Another real world example that this PR would “break”: at my last job writing React, we had a commonly used component for text styling called |
@andrewbranch one way we could be smarter there is to not show types in places where only values are legal to write, or to offer import auto-fixes when a name resolves to a type but not a value but a value is auto-importable |
@RyanCavanaugh unfortunately in this case, |
My feeling is that this is now worse than the status quo, and I like #31893 better. |
Fixes #30713
Locals already shadow auto-import suggestions, but people were having issues with globals like
clearTimeout
being auto-imported from'timers'
, for example.I'm not 100% sure this is the right thing to do. Thinking about the
clearTimeout
example:(Depending on the user’s tsconfig, they’ll likely have another ambient declaration in lib.dom.d.ts, but it becomes an overload with @types/node’s ambient declaration, so it’s not particularly relevant to the problem.)
Between these two declarations, I don't care about the one in
timers
at all. I never ever want to importclearTimeout
fromtimers
. Why?@types/node
on accident or indirectly, and the import actually won’t work because I'm writing for the browser.On the other hand, I can envision some scenarios where I have different motivations and desired outcomes:
There's a DOM global
File
, but as files are a pretty common thing to talk about in computing, it's also quite common to write a type or class calledFile
in your own project. In this case, I probably want to auto-import my ownFile
class. The global gets ranked higher, but at least I can press the down arrow key and get the auto-import. If we merge this PR, nothing calledFile
will ever be auto-importable (in a program that has lib.dom.d.ts).Observations:
clearTimeout
declarations in@types/node
are truly the same runtime function, but it seems highly likely since they’re both in@types/node
.file.ts
than for a module we found innode_modules
liketimers
.@types/node
is special, because it’s the only exception I can think of to the rule: “If I have a package.json, I do not want to auto-import from any package not listed in it.”Possible solutions:
node_modules
clearTimeout
fromtimers
gets hidden because it’s in@types/node
and so is a global one.