Nominal vs Structural Typing
Nominal typing decides compatibility by declared identity: a value fits a type only if it was declared to be that type. Structural typing decides by shape. TypeScript is structural, with a few deliberate exceptions.
Detailed Explanation
Languages answer "are these two types the same?" in one of two ways.
A nominal system answers by name. In Java or C#, a class named Meters and a class named Miles with identical fields are different types forever. To pass a class where an interface is expected, you must write implements on the class. Identity comes from the declaration, so the compiler enforces the distinctions you bothered to name.
A structural system answers by shape. TypeScript, Go's interfaces, and OCaml's object types work this way. If the members line up, the types are compatible, and nothing needs to be declared in advance.
| Nominal (Java, C#, Rust) | Structural (TypeScript, Go) | |
|---|---|---|
| Compatibility from | declaration identity | member shape |
| Opt in required | yes, implements/extends | no |
| Same shape, different name | incompatible | interchangeable |
| Describing code you do not own | awkward | natural |
TypeScript chose structural typing because it had to describe JavaScript that already existed. Millions of lines of untyped code return plain objects that were never declared to implement anything. A nominal checker could not have typed them without rewriting them.
The price is that meaning is invisible to the compiler. A UserId alias and an OrderId alias that are both string are the same type. Swapping them is a real production bug and a non-event for the type checker.
There is one exception TypeScript adds to pure structural assignability. When you assign a fresh object literal to a typed target, unknown properties are rejected. Passing the same object through a variable first is accepted, because that value may legitimately have been built for other purposes. The check exists to catch typos in config objects, not to make the system nominal. Knowing this explains an error that otherwise looks arbitrary.
When you genuinely need two structurally identical types to be incompatible, add a property that only your constructor function can produce. Intersect the underlying type with a phantom field, then funnel all creation through a single function. The field never exists at runtime — types are erased, so a branded UserId is a plain string in the emitted JavaScript, with zero cost in memory or speed.
Branding is not free in other ways. Every value entering the branded world needs an assertion, which means you have to trust one small function instead of the whole codebase. Error messages mention the brand property and get noisy. Use it for identifiers, units, and validated strings, not for every alias in the file.
Two nominal-ish features already ship in the language: classes with private or protected members compare by declaration rather than shape, and string enum members cannot be produced from an equivalent string literal.
Code Examples
1. Structural typing does not distinguish aliases of the same underlying type.
// 1. Structural typing does not distinguish aliases of the same underlying type.
type UserId = string;
type OrderId = string;
declare function loadUser(id: UserId): void;
const orderId: OrderId = "ord_918";
loadUser(orderId); // No error. Both types are just string.
// In Java or C#, distinct wrapper classes here would be rejected at compile time.2. Excess property checks apply to fresh literals only.
// 2. Excess property checks apply to fresh literals only.
interface RequestOptions {
retries?: number;
timeoutMs?: number;
}
const direct: RequestOptions = { retries: 3, timeoutMS: 500 };
// Error: Object literal may only specify known properties, but 'timeoutMS' does not exist
// in type 'RequestOptions'. Did you mean to write 'timeoutMs'?
const built = { retries: 3, timeoutMS: 500 };
const indirect: RequestOptions = built; // No error: not a fresh literal, and the shape fits.3. Branded types buy nominal behaviour inside a structural system.
// 3. Branded types buy nominal behaviour inside a structural system.
declare const brand: unique symbol; // a key nobody else can write
type Brand<T, Name extends string> = T & { readonly [brand]: Name };
type CustomerId = Brand<string, "CustomerId">;
type InvoiceId = Brand<string, "InvoiceId">;
// One assertion, in one place, after whatever validation you trust.
function asCustomerId(raw: string): CustomerId {
if (!raw.startsWith("cus_")) throw new Error("not a customer id");
return raw as CustomerId;
}
declare function charge(id: CustomerId, cents: number): void;
const invoice = "inv_42" as InvoiceId;
charge(invoice, 1299);
// Error: Argument of type 'InvoiceId' is not assignable to parameter of type 'CustomerId'.
charge(asCustomerId("cus_7"), 1299); // fine
// At runtime both values are ordinary strings; the brand is erased.Common Gotchas
A type alias is not a new type
`type Email = string` documents intent and enforces nothing. Anywhere an `Email` is expected, any string is accepted. Brand it if the distinction has to hold.
The excess property check is easy to defeat by accident
moving an object literal into a variable removes the check. That is not a workaround to reach for; it usually means a typo is now silently ignored.
Branding leaks into error messages and JSON
the phantom property shows up in compiler output and in inferred types shown by your editor. It never appears at runtime, so `JSON.stringify` and equality checks behave exactly as they did for the unbranded value.
Private members make classes nominal without warning
two classes with the same public API but separate `private` fields are not interchangeable, even though everything visible matches.