Challenge - 5 Problems
Phantom Types Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this TypeScript phantom type example?
Consider this TypeScript code using phantom types to distinguish between different units of measurement. What will be logged to the console?
Typescript
type Meter = { readonly __brand: unique symbol };
type Second = { readonly __brand: unique symbol };
function speed(distance: number & Meter, time: number & Second): number {
return distance / time;
}
const dist = 100 as number & Meter;
const t = 20 as number & Second;
console.log(speed(dist, t));Attempts:
2 left
💡 Hint
Phantom types don't affect runtime values but help TypeScript check types.
✗ Incorrect
The phantom types Meter and Second are used only at compile time to ensure correct units. At runtime, they are just numbers. So the division 100 / 20 equals 5.
🧠 Conceptual
intermediate1:30remaining
Why use phantom types in TypeScript?
Which of the following best explains the main purpose of phantom types in TypeScript?
Attempts:
2 left
💡 Hint
Think about how phantom types affect the compiled JavaScript.
✗ Incorrect
Phantom types add compile-time type information without changing the runtime code, improving type safety without overhead.
🔧 Debug
advanced2:30remaining
Identify the error in this phantom type usage
This TypeScript code tries to use phantom types to prevent mixing currencies. What error will occur when compiling?
Typescript
type USD = { readonly __brand: unique symbol };
type EUR = { readonly __brand: unique symbol };
function addUSD(a: number & USD, b: number & USD): number & USD {
return (a + b) as number & USD;
}
const priceUSD = 10 as number & USD;
const priceEUR = 15 as number & EUR;
const total = addUSD(priceUSD, priceEUR);Attempts:
2 left
💡 Hint
Check the types of arguments passed to addUSD.
✗ Incorrect
The function addUSD expects both arguments to be of type number & USD. Passing priceEUR of type number & EUR causes a compile-time type error.
📝 Syntax
advanced2:00remaining
Which option correctly defines a phantom type in TypeScript?
Select the correct way to define a phantom type to distinguish between Kilograms and Pounds.
Attempts:
2 left
💡 Hint
Phantom types use intersection with unique symbols for branding.
✗ Incorrect
Option A uses intersection with unique symbols, which creates distinct phantom types without runtime cost.
🚀 Application
expert3:00remaining
How many items are in the resulting map after using phantom types as keys?
Given this TypeScript code using phantom types as keys in a Map, how many entries does the map contain after execution?
Typescript
type UserId = string & { readonly __brand: unique symbol };
const user1 = 'abc123' as UserId;
const user2 = 'abc123' as UserId;
const map = new Map<UserId, string>();
map.set(user1, 'Alice');
map.set(user2, 'Bob');
console.log(map.size);Attempts:
2 left
💡 Hint
Phantom types do not change the runtime value or identity of strings.
✗ Incorrect
Both user1 and user2 are the same string value at runtime, so the second set overwrites the first. The map size is 1.