Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Merged
andrewbranch merged 8 commits intomicrosoft:masterfromandrewbranch:type-only
Jan 27, 2020
Merged

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:

importtype{PathLike}from'fs'constdir: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:

importtype{PathLike}from'fs'constdir=newDir/* | 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, Ireally 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

vicary and michaelhays reacted with thumbs up emojiorta reacted with heart emoji
Copy link
Contributor

@mjbvzmjbvz 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!

andrewbranch reacted with thumbs up emoji
Copy link
Member

@sheetalkamatsheetalkamat 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
MemberAuthor

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.

sheetalkamat reacted with thumbs up emoji
@andrewbranchandrewbranch merged commite1bce18 intomicrosoft:masterJan 27, 2020
@andrewbranchandrewbranch deleted the type-only branchJanuary 27, 2020 20:53
@DimitryDushkin
Copy link

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

@andrewbranch
Copy link
MemberAuthor

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

cyberalien, paambaati, witoszekdev, manzoorwanijk, JustFly1984, g-otn, ValeriiVasin, valentinpalkovic, gorazu, dgkimpton, and 33 more reacted with thumbs up emojipaambaati, ycaptain, valentinpalkovic, gorazu, Elia-Darwish, holbrookdev, michaelhays, Alexis-Bize, fivemru, mubaraqwahab, and 21 more reacted with heart emoji

@DimitryDushkin
Copy link

Thank you@andrewbranch !

andrewbranch, cyberalien, and lpolon reacted with thumbs up emoji

@ghost
Copy link

ghost commentedNov 15, 2021
edited by ghost
Loading

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 haveimportsNotUsedAsValues: "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 commentedJun 2, 2023

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

reubendrummond, dannyskoog, SMhdAsadi, dnlsandiego, michaelhays, tbazin, DianomiJH, SulaimanYaacob, kaikun213, mnvr, and 2 more reacted with thumbs up emoji

@Hemaolle
Copy link

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

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

DianomiJH and chris-feist reacted with thumbs up emoji

mnvr added a commit to mnvr/mrmr.io that referenced this pull requestJan 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
@microsoftmicrosoft locked asresolvedand limited conversation to collaboratorsOct 21, 2025
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.

Reviewers

@sheetalkamatsheetalkamatsheetalkamat approved these changes

@mjbvzmjbvzmjbvz approved these changes

@DanielRosenwasserDanielRosenwasserAwaiting requested review from DanielRosenwasser

Assignees

@andrewbranchandrewbranch

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Auto import adds value import to type-only import statement

7 participants

@andrewbranch@DimitryDushkin@Kos@Hemaolle@sheetalkamat@mjbvz@typescript-bot

[8]ページ先頭

©2009-2025 Movatter.jp