ReviseAlgo Logo

Advanced TypeScript Patterns

Phantom Types

Attach type-level generic markers to types without runtime overhead.

Last Updated: July 29, 2026 10 min read

A Phantom Type is a generic type parameter that appears on the type level declaration but is never used in any instance property values.

1. Introduction & Architecture

2. Deep Dive & Core Concepts

Phantom types allow encoding domain state (such as Validated vs Unvalidated, or USD vs EUR) into type wrappers to prevent illegal domain operations.

3. Basic Code Example

4. Advanced Production Patterns

5. Interactive Code Playground

console.log("Phantom types add type-safe state tracking with zero runtime overhead.");

6. Common Pitfalls & Edge Cases

Note: Phantom types emit no extra JavaScript code at runtime, providing pure compile-time domain safety.

7. Interview Q&A & Quizzes

Q: What is a phantom type parameter?

A: A generic type parameter used strictly for compile-time type verification that does not correspond to any physical instance property.

8. Summary Comparison Table

Type TechniquePurposeRuntime Cost
Phantom TypeEncodes compile-time state markersZero (Erased)
| Runtime Property | Encodes runtime state flags | Object memory cost |