-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Type-only auto imports #36412
Type-only auto imports #36412
Conversation
… to preserve import specifier formatting
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.
Description of the new behavior sounds good to me. Thanks for looking into this!
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.
Apart from question on the test change looks good.
// @target: esnext | ||
|
||
// @Filename: /a.ts | ||
////export interface Car {} |
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.
Why was this deleted, is it because similar test already exists?
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.
Yeah, it was actually verifying a behavior that we actually don’t want anymore (using a type-only import for new imports of types), and reversing that behavior makes it redundant with many others.
Hey! Could you please clarify how to enable auto-import with |
An auto import will only be written as a type-only import if not doing so would result in an error. For that to be true, your tsconfig must have |
Thank you @andrewbranch ! |
Awesome! It works, but.. the compiler gets stuck after such an error raises up and any change in a document does not invoke compiling. Am I the only one? |
For those who find their way here from web search: The |
|
This does two things: 1. It is now an error to import a type without specifying the type modifier. 2. But even better, this flag causes VSCode to automatically insert the type modifier instead of me having to go back and manually add that everytime. References: - microsoft/TypeScript#36412 (comment) - https://www.typescriptlang.org/tsconfig#verbatimModuleSyntax
Auto imports will not use type-only imports, unless an import specifier can be added to an existing type-only import, and usage of the import occurs at a position where a type-only alias can be used:
On the other hand, if the import can be added to an existing type-only import, but is used in a position that is invalid for a type-only alias, the type-only import will be converted to a regular import:
From feedback in #36038, it sounds like we may also need to follow up and create a “hard mode” where an existing type-only import never converts to a regular import and blocks regular imports from that module from being added by auto-import. Such a hardline approach would probably frustrating, unexpected, and unnecessary for most users (as evidenced by the fact that #36002 was filed as a bug immediately) if enabled by default, but it’s justifiable to say “if I go through the trouble to write a type-only import, I really want to make sure I don’t regular-import that module into this one.”
Fixes #36002