`import type` in TypeScript
See original GitHub issueTypeScript 3.8 will support a new import type
construct. While there’s already some logic for similar stuff (Flow has its own import type
), TypeScript’s has a few extra restrictions.
-
import type
can only be used on imports with named imports or with a default import - but not both.import type FooDefault, { Bar, Baz } from "module"; // error!
-
Even though
import type
creates no emit, animport type
functionally shadows variables from the outer scope, and using these declarations in a value position that will be emitted causes an error.import type { RegExp } from "special-regexp-module"; // Okay - RegExp won't be used at runtime here. declare class Foo extends RegExp { } // Okay - RegExp won't be used at runtime here. let r: typeof RegExp; // Error! RegExp is imported via import type and cannot be used as a value here. new RegExp()
See more at
Issue Analytics
- State:
- Created 4 years ago
- Reactions:42
- Comments:22 (16 by maintainers)
Top Results From Across the Web
Documentation - TypeScript 3.8
import type only imports declarations to be used for type annotations and declarations. It always gets fully erased, so there's no remnant of...
Read more >Do I need to use the "import type" feature of TypeScript 3.8 ...
import type only imports declarations to be used for type annotations and declarations. It always gets fully erased, so there's no remnant of...
Read more >Import a Type from Another file using TypeScript
To import a type from another file in TypeScript: ... Here is an example of exporting a type from a file called another-file.ts...
Read more >Leveraging Type-Only imports and exports with TypeScript ...
Well, as the TS release notes explain, by importing an element using “import type”, it tells the compiler that the element is only...
Read more >type-only imports — A new TypeScript feature that benefits ...
type -only imports — A new TypeScript feature that benefits Babel users · Adds static type checking to code you would traditionally write...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I thought this was the TypeScript repository when I posted my previous comment.
The explorer suggests we use
importKind: 'type'
for type imports andimportKind: 'value'
for value imports.