-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Support a user preference for preferring the type
keyword in auto-imports
#54664
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
Comments
If there is some way to accomplish this in current versions, fantastic, but I cannot find anything in the docs. |
|
That's correct, I believe we chose not to have specific user options here, in favor of performing this based on |
type
keyword in auto-imports
Why not always do it? Are there some drawbacks to this syntax? |
The docs indicate there are some other side-effects of using How can you get type imports without other side effects? |
For folks needing |
Actually now that I've written this, I would push for this to not be a user preference (or at least be fixed in this mode, in addition to being a preference in other cases) and instead be "fixed". If you're using this specific combination of flags, you're going to always be fighting TS's auto-imports, when you go to do a full build. |
@benasher44 it's unclear what you're referring to - can you file a separate issue if you think there's a bug? |
I think there's a misunderstanding. The docs are pointing out to the fact that This is in contrast to the original behavior - where if you imported something and never used it as a JavaScript value, then TypeScript would silently drop the import. |
For example:
My goal is to make TS automatically add |
Okay, gotcha - that makes sense. |
If this ever happens it’s a bug. I’m pretty open to a user preference for type-only imports. Auto-imports had to get a bit more sophisticated around type-only imports with |
Got it! Filed #54717 |
Nice, NodeNext + isolated modules will be the flags that affect things? I'm fine with that approach, so long as we can get it properly documented. |
The flags that affect type-only auto imports are |
Seems good. Although I would call it a bit more than a stylistic preference because some build systems require it. |
Is there any situation where it is desirable to do |
Which ones?
In the vast majority of cases, there’s no observable difference between these two. Some might say it’s desirable not to add unnecessary syntax. Some might say it’s desirable to be explicit about the intended usage. So, it’s a matter of taste, and we generally try not to change long-standing default editor behaviors over matters of taste. |
This whole issue was prompted by migrating something to vite. Upon further investigation, it seems to be more focused and related to re-exporting types, e.g.: // types/foo.ts
export type Foo: number;
// types/index.ts
export { Foo } from './foo';
// Error: Re-exporting a type when 'isolatedModules' is enabled requires using 'export type'. (tsserver 1205) It sounds like I have a different issue to solve. I don't imagine TS server can solve this? eslint perhaps? |
|
Got it. Since exports are not generated by the TS server, I'm going to close this request. Thank you, all. |
Suggestion
Add support in the TS server to include
import type
and inlinetype
imports when auto-importing.🔍 Search Terms
✅ Viability Checklist
My suggestion meets these guidelines:
⭐ Suggestion
See above
📃 Motivating Example
Many build systems require isolated modules (e.g., esbuild), and often require that types be thusly marked when imported. According to my understanding, that is only done manually, as enabling
isolatedModules
doesn't seem to affect things.💻 Use Cases
Work better with isolated module build system requirements. One could also argue that legibility is improved with
type
imports.The text was updated successfully, but these errors were encountered: