Skip to content
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

Merged
merged 8 commits into from
Jan 27, 2020
Merged

Type-only auto imports #36412

merged 8 commits into from
Jan 27, 2020

Conversation

andrewbranch
Copy link
Member

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:

import type { PathLike } from 'fs'

const dir: Dir/* | adds 'Dir' to existing type-only import */

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:

import type { PathLike } from 'fs'

const dir = new Dir/* | adds 'Dir' to existing import and deletes 'type' */

GIF showing the two preceding code examples in action

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.”

Screenshot of Minecraft “Hardcore Mode” enabled with setting description “World is deleted upon death.” I have crossed out and annotated it to read “Project is deleted upon value import.”

Fixes #36002

Copy link
Contributor

@mjbvz mjbvz left a 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!

Copy link
Member

@sheetalkamat sheetalkamat left a 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 {}
Copy link
Member

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?

Copy link
Member Author

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.

@andrewbranch andrewbranch merged commit e1bce18 into microsoft:master Jan 27, 2020
@andrewbranch andrewbranch deleted the type-only branch January 27, 2020 20:53
@DimitryDushkin
Copy link

Hey! Could you please clarify how to enable auto-import with type for first import? AFAIK this pull can auto-import with type if such import already exists.

@andrewbranch
Copy link
Member Author

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 importsNotUsedAsValues: "error", and the place where you’re performing the auto-import must be a type position.

@DimitryDushkin
Copy link

Thank you @andrewbranch !

@ghost
Copy link

ghost commented Nov 15, 2021

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 importsNotUsedAsValues: "error", and the place where you’re performing the auto-import must be a type position.

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
Copy link

Kos commented Jun 2, 2023

For those who find their way here from web search: The importsNotUsedAsValues tsconfig option is now deprecated, and the new one to look at is verbatimModuleSyntax. I confirm vscode generates import type after enabling this flag.

@Hemaolle
Copy link

For those who find their way here from web search: The importsNotUsedAsValues tsconfig option is now deprecated, and the new one to look at is verbatimModuleSyntax. I confirm vscode generates import type after enabling this flag.

verbatimModuleSyntax was introduced in TS 5.0, so check your TS version before taking that option into use.

mnvr added a commit to mnvr/mrmr.io that referenced this pull request Jan 6, 2024
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Auto import adds value import to type-only import statement
7 participants