Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A type import in TypeScript is a module resolution mechanism that allows developers to import type declarations—such as interfaces, type aliases, and type signatures—from an external module exclusively for static type checking. By explicitly designating an import as type-only, the TypeScript compiler guarantees that the import statement is completely erased during the transpilation phase, resulting in zero runtime JavaScript output.

Syntax

TypeScript supports two syntactical forms for type imports: 1. Top-Level Type Import (TypeScript 3.8+) Applies the type modifier to the entire import statement. All named imports within the destructured block are treated strictly as types.
import type { User, Session } from './models';
import type DefaultConfig from './config';
import type * as Types from './types';
2. Inline Type Import (TypeScript 4.5+) Applies the type modifier to individual named imports. This allows mixing runtime value imports and type-only imports within a single AST node.
import { fetchUser, type User, type Session } from './api';

Compiler Mechanics and Type Erasure

  • Forced Elision: Standard import statements in TypeScript are subject to implicit import elision; the compiler attempts to drop the import if it detects the imported bindings are only used in type positions. However, import type bypasses this heuristic, forcing the compiler to drop the import statement entirely during JavaScript emission regardless of context.
  • Value Position Restriction: Bindings imported via import type cannot be evaluated at runtime. Attempting to use a type-imported binding in a value position (e.g., instantiating a class, passing it as an argument, or using it in a runtime typeof check) will result in a compiler error (TS1361: 'X' cannot be used as a value because it was imported using 'import type').
  • Class Behavior: When a class is imported using import type, TypeScript extracts only its structural shape (instance properties and methods). The runtime constructor function is not imported. Consequently, the class can be used for type annotations but cannot be instantiated via the new keyword or subclassed via the extends clause.

Compiler Configuration

The enforcement and behavior of type imports interact directly with specific tsconfig.json compiler options:
  • verbatimModuleSyntax: When enabled, TypeScript disables implicit import elision heuristics. Any standard import is preserved in the emitted JavaScript exactly as written. To prevent a type-only module from being emitted and causing runtime ReferenceErrors, developers must use the import type syntax.
  • isolatedModules: When using single-file transpilers (such as Babel, SWC, or esbuild), the transpiler cannot perform cross-file type analysis to determine if an ambiguous import is a type or a value. import type provides the explicit AST signal required by these external tools to safely strip the import without needing the full TypeScript type-checker context.
Master TypeScript with Deep Grasping Methodology!Learn More