Phantom Type

Noun · Development

Definitions

  1. A type parameter that appears in a type's definition but is not used in any of its fields — it exists only to carry compile-time information. Used to encode state machine transitions, units of measurement, or validated/unvalidated states in the type system, catching logic errors at compile time.

    In plain English: A type trick that adds invisible labels to data so the compiler can catch mistakes, without any runtime cost.

    Example: "UserId<Validated> and UserId<Unvalidated> are the same struct at runtime, but the phantom type prevents passing unvalidated IDs to secure functions."

Related Terms