← All Exercises

Phantom Types for Safety

advancedtypescript~30min

Background

Phantom types add type-level distinctions without runtime cost, useful for state machines and validation tracking.

Problem

Create phantom types for distinguishing verified and unverified user IDs, with a function that only accepts verified IDs.

Input

A user ID tagged with a phantom type.

Output

A function that only accepts verified IDs, rejecting unverified ones at compile time.

Sample tests

Sample 1

Input: verifiedUserId
Expected: Access granted

Your solution

Learning objective

Learn phantom types for compile-time state tracking without runtime overhead.