TypeScript Reference ยท Keywords and Declarations
The import type declaration
Reference for TypeScript import type and inline type modifiers: erased imports, import() types, verbatimModuleSyntax, and when type-only imports matter.
import type imports a name that can be used only in type positions. The whole statement is erased from the JavaScript output, so it can never trigger a module load, create a circular dependency at runtime or keep a large module in your bundle.
The same idea is available per name with an inline modifier, import { type A, b } from "m". In that form the statement stays when it also imports values, but A is dropped. Type-only imports make intent explicit and are required under verbatimModuleSyntax for anything that is purely a type.
import type { Server } from "node:http"; import { createServer, type IncomingMessage } from "node:http";
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Syntax
import type Default from "m"; import type { A, B as C } from "m"; import type * as NS from "m"; import { type A, value } from "m"; type T = import("m").Exported; type Whole = typeof import("m");
| Form | Description |
|---|---|
import type X |
Imports only for use in types. Using X as a value is an error. |
import type * as NS |
A namespace usable in type positions, including typeof NS.value. |
{ type A } |
Marks one specifier as type-only inside a normal import. |
import("m").T |
An import type: refers to a type of a module without an import statement. |
Edge cases and errors:
- Using a type-only import as a value, such as
new X()for an imported class, is error TS1361. - A class imported with
import typecan still be used in annotations, but not withnew,instanceoforextendsin expressions. - With
verbatimModuleSyntax,import { A } from "m"whereAis only a type is an error asking you to addtype. - A statement made only of inline type modifiers,
import { type A } from "m", is kept asimport {} from "m"underverbatimModuleSyntax(a side-effect import), but fully erased withimport type { A }.
Usage notes
- Use
import typewhen a module is needed only for annotations. It prevents accidental runtime cycles. importsNotUsedAsValuesandpreserveValueImportswere replaced byverbatimModuleSyntax; use that in new projects.- Transpilers that process one file at a time (esbuild, swc, Babel, Node's type stripping) cannot tell types from values across files, so
import typeis how you tell them what to drop. - Decorators with
emitDecoratorMetadatacan need the value import for metadata. A type-only import can break them. import typedoes not change type checking. It only changes emit and enforces your intent.