Sub topic 2
The Problem with Plain String and Number Aliases
Prerequisites
This chapter assumes basic TypeScript syntax, including type aliases and functions. You should also know that TypeScript checks types at compile time, while JavaScript values exist at runtime.
Type aliases describe meaning, but do not create new types
A type alias gives another name to an existing type. It improves readability, but it does not make the type distinct.
type UserId = string;
type OrderId = string;
function loadOrder(id: OrderId): void {
console.log("loading order", id);
}
const userId: UserId = "u_123";
loadOrder(userId); // Compiles, even though this is a user IDUserId and OrderId are both aliases for string. TypeScript compares their underlying structure, so a value of either type can be passed wherever a string is expected.
The same problem occurs with numbers:
type Meters = number;
type Seconds = number;
function reportDistance(distance: Meters): void {
console.log(`${distance} meters`);
}
const elapsed: Seconds = 30;
reportDistance(elapsed); // Compiles, but the value represents secondsThe compiler sees two numbers. It cannot infer that one number represents a distance and the other represents a duration.
Structural typing and primitive obsession
TypeScript uses structural typing. A value is compatible with a type when it has the required structure. For primitive aliases, that structure is simply the primitive itself.
This design is convenient for ordinary JavaScript code, but domain values often share a representation while having different meanings.
| Domain value | Runtime representation | Mistake the compiler misses |
|---|---|---|
| `UserId` | `string` | Passing a user ID to an order function |
| `OrderId` | `string` | Passing an order ID to a user function |
| `Meters` | `number` | Treating a duration as a distance |
| `Seconds` | `number` | Adding incompatible measurements |
This pattern is sometimes called primitive obsession. It means important domain concepts are represented only by general-purpose primitives such as strings and numbers. The code looks simple, but every function must rely on naming and developer discipline.
Why the error can be expensive
The compiler may approve the call, but the mistake can still cause a runtime failure or incorrect data lookup. For example, an API request might return a missing record, update the wrong record, or expose an authorization bug.
Names help humans read code:
function deleteUser(userId: UserId): void {
console.log("deleting user", userId);
}
function deleteOrder(orderId: OrderId): void {
console.log("deleting order", orderId);
}However, names do not protect calls. Both functions still accept plain strings, and either alias can be passed to either function.
The goal is not to reject every string or number. A raw value often enters the program from a form, URL, database, or API. The goal is to distinguish values after the program has established what they mean. Branded types add compile-time information to the existing primitive representation. The next chapter introduces that technique.
Exercise
Why does TypeScript allow a value of type UserId to be passed to a function expecting OrderId when both aliases refer to string?
Show answer
TypeScript compares their underlying structure. Both aliases describe the same primitive type, string, so neither alias is distinct from the other.