Don’t show auto-import suggestion if there’s a global by the same name #31065
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.