Import Elision and Side Effects
What you'll learn: How TypeScript removes type-only imports during compilation and why this matters for code that relies on import side effects.
The Core Idea
When TypeScript compiles your code to JavaScript, it performs import elision — automatically removing imports that appear to only reference types. This is logical since types don't exist at runtime, but it creates a hidden danger: if your import had side effects (like registering a plugin or polyfill), removing it breaks your code.
How Import Elision Works
TypeScript analyzes each import statement. If it sees you're only using the imported value in type positions (annotations, type assertions, etc.), it assumes the import can be safely deleted:
// You write:
import { EventEmitter } from 'events';
let emitter: EventEmitter;
// TypeScript compiles to:
let emitter;
// Import vanished! EventEmitter only used as a type.
The Side Effect Problem
Some imports exist purely for their side effects — they execute code when loaded:
// polyfills.ts runs code when imported
import './polyfills';
// This import stays because it has no bindings.
// TypeScript preserves "bare" imports.
But this breaks:
import { Polyfill } from './polyfills';
let _check: Polyfill; // Only used as a type
// Import removed! Polyfill code never runs.
Protecting Side Effects
Use import type to explicitly mark type-only imports, and keep value imports separate:
import './polyfills'; // Side effect preserved
import type { Polyfill } from './polyfills'; // Explicitly type-only
Or reference the value to prevent elision:
import { registerPlugin } from './plugin';
registerPlugin; // Value reference prevents removal
Key Takeaway: TypeScript automatically removes imports used only as types, which can accidentally eliminate necessary side effects. Separate type imports from side-effect imports, or ensure value imports are actually referenced in runtime code.