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
clearTimeoutbeing auto-imported from'timers', for example.I'm not 100% sure this is the right thing to do. Thinking about the
clearTimeoutexample:(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
timersat all. I never ever want to importclearTimeoutfromtimers. Why?@types/nodeon 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 calledFilein your own project. In this case, I probably want to auto-import my ownFileclass. 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 calledFilewill ever be auto-importable (in a program that has lib.dom.d.ts).Observations:
clearTimeoutdeclarations in@types/nodeare truly the same runtime function, but it seems highly likely since they’re both in@types/node.file.tsthan for a module we found innode_modulesliketimers.@types/nodeis 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_modulesclearTimeoutfromtimersgets hidden because it’s in@types/nodeand so is a global one.