Course contentsShow
← TypeScript
Lesson 1261 of 1,79629. The Build PipelineFree lesson

Type vs Value Ambiguity

Resolving cases where identifiers could be types or values and how bundlers handle them.

Type vs Value Ambiguity

What you'll learn: How TypeScript resolves cases where an identifier could be either a type or a runtime value, and why bundlers care about this distinction.

The Dual Nature Problem

TypeScript has a unique challenge: some identifiers can exist in both the type space and the value space simultaneously. A class, for example, acts as both a constructor function (a value you can call with new) and a type annotation (describing the shape of instances).

class User {
  name: string;
}

// As a type:
const u: User = { name: "Alice" };

// As a value:
const instance = new User();

The same identifier User means two different things depending on context. TypeScript the compiler understands this perfectly during type-checking.

Why Bundlers Struggle

Bundlers like esbuild, swc, and Webpack with ts-loader need to strip types from your code without performing full type analysis. When they encounter an import, they must decide: "Is this a type-only import that should be removed, or does it import runtime code?"

import { Config } from './config';

const value: Config = { port: 3000 };

Is Config used only as a type here? If so, the bundler can safely remove the import. But if Config is also a class or object used as a value elsewhere, removing it breaks the code.

How TypeScript Helps

With isolatedModules enabled (which you learned earlier), TypeScript enforces that you must make your intent clear:

// Explicit type-only import
import type { Config } from './config';

// Mixed: value and type
import { User, type UserID } from './user';

The type modifier tells both TypeScript and bundlers: "This is never used as a runtime value." Bundlers can confidently erase these imports without analyzing the entire project.

Resolution Strategy

When the intent isn't explicit, bundlers use heuristics:

  • Used only in type positions? Probably safe to remove
  • Used with new, called as a function, or accessed as an object? Keep it

But these heuristics aren't foolproof, which is why verbatimModuleSyntax and explicit import type exist—to eliminate ambiguity at the source.

Key Takeaway: Type vs value ambiguity arises because identifiers can exist in both spaces; bundlers rely on explicit import type syntax or isolatedModules rules to safely strip types without deep analysis.