Nominal Types

This lesson talks about nominal types, a concept that is not part of TypeScript, but can be emulated easily.

Structural vs nominal typing

The majority of programming languages traditionally associated with static typing (Java, C#, C++) follow an approach called nominal typing. In a nominal type system every type is unique. Imagine that you have created two interfaces with exactly the same properties. In TypeScript, it’s perfectly fine to assign an instance of one of these interfaces to a variable typed using the other one. However, in languages with nominal typing, that wouldn’t be possible. TypeScript doesn’t use nominal typing, instead is uses structural typing.

Press + to interact
interface Cat {
name: string;
breed: string;
}
interface Dog {
name: string;
breed: string;
}
const cat: Cat = { name: 'Filemon', breed: 'Chartreux' };
let dog: Dog = cat; // ✅ No error

The TypeScript is different because of the need for compatibility with JavaScript. With nominal typing, even the line below would result in an error: ...