Most programming languages use either nominal typing or structural typing.
For example:
interface Cat {
legs: number;
}
interface Dog {
legs: number;
}
- In nominal typing,
Cat
andDog
are different types, because their names are different. - In structural typing,
Cat
andDog
are the same type, because their structure is the same.
Nominal typing is more natural to developers, because it's how our societies work. In all walks of life, we're used to the idea of using names to tell things apart.
Nominal typing is more robust than structural typing. It's more specific: you have to pass in the exact type that was intended. You can't accidentally pass in the wrong type.
Typescript doesn't support nominal typing, because Javascript engines use structural typing.
We can use type branding to emulate nominal typing in Typescript.