镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Type-only auto imports - #36412

Merged
Andrew Branch (andrewbranch) merged 8 commits into
microsoft:masterfrom
andrewbranch:type-only
Jan 27, 2020
Merged

Andrew Branch (andrewbranch) merged 8 commits into
microsoft:masterfrom
andrewbranch:type-only

Conversation

@andrewbranch

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

@mjbvz Matt Bierner (mjbvz) left a comment

Copy link
Copy Markdown

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
Copy Markdown
Member

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
Copy Markdown
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
Copy Markdown
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
Andrew Branch (andrewbranch) merged commit e1bce18 into microsoft:master Jan 27, 2020
@DimitryDushkin

Copy link
Copy Markdown

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

Thank you Andrew Branch (@andrewbranch) !

@ghost

Deleted user (ghost) commented Nov 15, 2021 •

Copy link
Copy Markdown

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

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

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.

Manav Rathi (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
@microsoft Microsoft (microsoft) locked as resolved and limited conversation to collaborators Oct 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

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