Search⌘ K
AI Features

Nominal Types

Explore the concepts of nominal and structural typing in TypeScript, and understand how to implement branded types to prevent type mix-ups. This lesson helps you write safer code by creating unique type identifiers and using type guards to enforce non-empty strings.

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. ...