- Notifications
You must be signed in to change notification settings - Fork13.2k
Type-only auto imports#36412
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Type-only auto imports#36412
Uh oh!
There was an error while loading.Please reload this page.
Conversation
… to preserve import specifier formatting
mjbvz left a comment
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!
sheetalkamat left a comment
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 actuallydon’t want anymore (using a type-only import for new imports of types), and reversing that behavior makes it redundant with many others.
DimitryDushkin commentedOct 12, 2020
Hey! Could you please clarify how to enable auto-import with |
andrewbranch commentedOct 12, 2020
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 |
DimitryDushkin commentedOct 12, 2020
Thank you@andrewbranch ! |
ghost commentedNov 15, 2021 • edited by ghost
Loading Uh oh!
There was an error while loading.Please reload this page.
edited by ghost
Uh oh!
There was an error while loading.Please reload this page.
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? |
Kos commentedJun 2, 2023
For those who find their way here from web search: The |
Hemaolle commentedAug 16, 2023
|
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, Ireally want to make sure I don’t regular-import that module into this one.”
Fixes#36002