Sub topic 1
Structural vs nominal typing
Prerequisites
You should know basic TypeScript syntax, including type annotations, interfaces, and type aliases. You should also be familiar with JavaScript objects, classes, and functions.
Two ways to compare types
A type system checks whether a value can be used in a particular place. For example, a function might require an object with x and y properties.
TypeScript uses structural typing. Structural typing compares the members of a value, such as its properties and methods. The declared name of the type does not usually matter.
A nominal type system compares declared identity. Two types are different because they have different names or declarations, even if they contain the same members. Java and C# use nominal rules for many class relationships.
| Type system | Main question |
|---|---|
| Structural | Does the value have the required shape? |
| Nominal | Does the value come from the required named type? |
In TypeScript, an object with the right shape can usually be assigned to a variable with an interface type. The object does not need to explicitly declare that it implements the interface.
TypeScript checks shape
Consider a function that accepts a Point. The function needs only two numeric properties. It does not need to know whether the value came from an interface, a class, or an object literal.
interface Point {
x: number;
y: number;
}
function logPoint(point: Point): void {
console.log(`${point.x}, ${point.y}`);
}
const objectPoint = {
x: 1,
y: 2,
label: "start",
};
logPoint(objectPoint); // OK
class Vector {
constructor(
public x: number,
public y: number,
) {}
}
logPoint(new Vector(3, 4)); // OKobjectPoint has at least the members required by Point, so it is assignable to Point. Vector also has x and y, so an instance of Vector is accepted. The class does not need an implements Point clause for this call to work.
This rule supports reusable APIs. A function can describe the capabilities it needs instead of requiring a particular class or inheritance hierarchy.
Structural typing is based on required members
When checking one object type against another, TypeScript asks whether every required member in the target type exists in the source type with a compatible type.
interface HasName {
name: string;
}
interface UserRecord {
name: string;
id: number;
}
const user: UserRecord = {
name: "Mina",
id: 42,
};
const named: HasName = user; // OKHasName requires only name. UserRecord provides name and an additional id, so the assignment is valid.
The direction matters. A HasName value cannot be assigned to UserRecord, because TypeScript cannot guarantee that it has the required id property.
declare const namedValue: HasName;
const record: UserRecord = namedValue;
// Error: property 'id' is missingAt runtime, TypeScript's type annotations disappear. Structural typing is therefore a compile-time rule. It does not add checks or metadata to JavaScript objects.
When structural typing is not enough
Structural compatibility can allow values that are technically valid but conceptually different. For example, both a UserId and an OrderId might be represented as strings. Plain string types cannot prevent those values from being mixed.
Classes can also introduce nominal-like behavior when they contain private or protected members. Those members must come from compatible declarations, even when the visible public shape is the same. Branded types provide another way to make related values distinct. These techniques are useful when names carry meaning that structure alone cannot express.
Exercise
Exercise
Why can an object with x and y properties be passed to a function expecting Point, even if the object was not created from Point?
Show answer
TypeScript uses structural typing. The object is assignable because it has the required members of Point with compatible types.