Prefer type correct import for add missing import for quick fixes
See original GitHub issueBug Report
From https://github.com/microsoft/vscode/issues/115370
🔎 Search Terms
- Quick fix
- auto import
🕗 Version & Regression Information
Tested version: 4.2.0-dev.20210201
⏯ Playground Link
N/A
💻 Code
For the project:
// a1.ts
export const abc = 123
// a2.ts
export const abc = '123'
// index.ts
function foo(x: string) { }
foo(abc); // trigger quick fix here
- Trigger the quick fix on
abc
infoo(abc)
🙁 Actual behavior
The import quick fixes are returned in file order:
![Screen Shot 2021-02-02 at 12 45 53 PM](https://user-images.githubusercontent.com/12821956/106660281-a1bb5880-6554-11eb-9e64-20f1c343aefb.png)
Accepting the first suggestion results in invalid code:
![Screen Shot 2021-02-02 at 12 46 42 PM](https://user-images.githubusercontent.com/12821956/106660334-b5ff5580-6554-11eb-8e4b-cdca3b18fb1b.png)
🙂 Expected behavior
In this case, we should sort the import for a2
before a1
since it has the correct type
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:6 (6 by maintainers)
Top Results From Across the Web
Auto-import quickfix : wrong strategy for import paths #15223
Actual behavior: With the new auto-import quick fix in TS2.2 (usable in VS Code), the suggested paths for missing imports are behaving strangely ......
Read more >Auto import | IntelliJ IDEA Documentation - JetBrains
Basic procedures to create and optimize imports in IntelliJ IDEA. Learn more how to import the missing import or XML namespace.
Read more >Visual Studio Code Automatic Imports - typescript
There are two types of auto imports: the add missing import quick fix which shows up as a lightbulb on errors: enter image...
Read more >Working with JavaScript in Visual Studio Code
These type checks also enable some exciting Quick Fixes for JavaScript, including Add missing import and Add missing property. Using type checking and...
Read more >Import or link to data in a text file - Microsoft Support
When you import fixed-width text files, Access does not give you the option of using the values in the first row as the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@RyanCavanaugh Yes, thinking about this more I agree this is probably not feasible for auto import suggestions right now.
For quick fixes though, the lightbulb generally should not produce invalid code so I think it does make sense to be smarter for those. Quick fixes are also being requested less often, so we don’t have the same performance concerns as with suggestions.
In VS Code, we also have an ‘auto fix’ command that will try to automatically apply the best quick fix to address a problem. It would be great if auto fix knew to use the type correct import by default instead of always defaulting to the first returned import suggestion
That distinction makes sense 👍