-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Add import type "mod" #36812
Copy link
Copy link
Open
Labels
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureThis means we'd like to hear from more people who would be helped by this featureIn DiscussionNot yet reached consensusNot yet reached consensusSuggestionAn idea for TypeScriptAn idea for TypeScript
Metadata
Metadata
Assignees
Labels
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureThis means we'd like to hear from more people who would be helped by this featureIn DiscussionNot yet reached consensusNot yet reached consensusSuggestionAn idea for TypeScriptAn idea for TypeScript
Search Terms
type-only import
Suggestion
The new type-only import syntax supports most variations of
importstatement but not this one:import type 'mod'It may seem stupid because no type is actually imported?! But it's in fact useful if
modcontains some global declarations that you need.Global declarations are currently only imported from:
typeroots(@types/*by default) ortypesconfigs; and referenced modules.This syntax is even more useful because it can't be emulated today. If you do(EDIT: see triple-slash comment)import 'mod'then typing works but TS assumes you import the module for side-effects and will not erase it.Use Cases
I encountered this today.
One package I use provides some dependency injection and basically defines the following module augmentation:
inject(x: 'myType'): MyTypewhich is a strongly typed overload of
inject<T = unknown>(x: string): T.Inject itself comes from another library so code that wants to use this import needs to do this:
Current work-arounds include:
import { MyType } from 'my-lib'but if you don't use it in code it'll be a warning;import 'my-lib'but it won't be erased;Add this lib to(EDIT: not really working, see my comment below for an alternative solution.)typerootsortypesand do nothing (might be better in some cases)./// <reference types="mod" />, which works but I think most people have forgotten about? (I have 😜)Checklist
My suggestion meets these guidelines: