Sub topic 1
Why TypeScript lets you mix up IDs: structural typing
TypeScript uses structural typing. This means it decides whether one value can be used as another type by comparing their structure, or shape. The name of a type alias does not usually matter.
This behavior is useful because it makes ordinary JavaScript objects easy to use. However, it can be unsafe when two values have the same representation but different meanings. User IDs and order IDs are a common example.
Prerequisites
You should know basic TypeScript syntax, including type aliases and functions. You should also understand that TypeScript types are checked at compile time, while JavaScript values exist at runtime.
Type aliases do not create new primitive types
A type alias gives another name to an existing type. It does not create a separate type.
type UserId = string;
type OrderId = string;
function loadOrder(id: OrderId): void {
console.log("loading order", id);
}
const userId: UserId = "u_123";
loadOrder(userId);This code compiles. UserId and OrderId are both aliases for string, so TypeScript treats userId as a valid argument for loadOrder.
The compiler cannot infer that the prefix "u_" means “user” and "o_" means “order.” Those meanings exist in your application, not in the structure of the values.
| Type declaration | What TypeScript sees |
|---|---|
| `type UserId = string` | `string` |
| `type OrderId = string` | `string` |
| `type User = { id: string }` | An object with an `id` property of type `string` |
Why structural typing behaves this way
Consider object types:
type User = {
id: string;
name: string;
};
type NamedRecord = {
id: string;
name: string;
};
function printRecord(record: NamedRecord): void {
console.log(record.id, record.name);
}
const user: User = {
id: "u_123",
name: "Mina",
};
printRecord(user);User can be passed to printRecord because it has every property required by NamedRecord with compatible types. The two type names do not need to match.
This is often called duck typing: if a value has the required shape, TypeScript accepts it. For object types, extra properties are usually allowed when a value comes from a variable. For primitive aliases such as UserId and OrderId, there is no extra structure at all, so the aliases are completely interchangeable.
The domain error
The code compiles, but it can still be wrong:
type UserId = string;
type OrderId = string;
function findOrder(id: OrderId): string {
return `order:${id}`;
}
const userId: UserId = "u_123";
const result = findOrder(userId);
console.log(result);The function receives a user ID where an order ID was expected. If the application uses these IDs to query a database or call an API, it may return the wrong record or fail in a confusing way.
The problem is sometimes called primitive obsession: representing many domain concepts with general primitive values such as strings and numbers. TypeScript checks that the value is a string, but it does not check which kind of string it represents.
A stricter type needs additional structure that distinguishes the concepts while preserving the underlying value. Branded types provide that distinction. They add a compile-time tag to a primitive so that UserId and OrderId are no longer interchangeable.
Exercise
Why does TypeScript allow a UserId to be passed to a function expecting an OrderId when both are defined as aliases of string?
Show answer
Type aliases do not create new primitive types. Both aliases resolve to string, so structural typing considers them compatible.