I knew TypeScript was structural, so I started there.
Start by clearly defining nominal and structural typing, then state that TypeScript uses structural typing by default. Use a concrete example like UserId and OrderId (both strings) to illustrate the risk, and demonstrate how to enforce nominal typing using branding or private fields. Emphasize the trade-offs and practical implications in a safety-critical context like Boeing.
Pro tip: Mention that in safety-critical systems like avionics, nominal typing can prevent unit mix-ups (e.g., meters vs feet), and show how TypeScript's branding technique can be used without runtime overhead.
Explain that nominal typing requires explicit declarations or inheritance to be compatible, while structural typing only cares about the shape of the data. Give a brief example of each.
Clarify that TypeScript uses structural typing by default, meaning types are compatible if their members are compatible, regardless of their names or origins.
Show two types that are structurally identical but represent different concepts, such as UserId and OrderId, both aliases for string. Explain how TypeScript would allow assigning one to the other, which could lead to bugs.
Demonstrate how to simulate nominal typing in TypeScript using branding (e.g., type UserId = string & { __brand: 'UserId' }) or private fields in classes. Show how this prevents accidental mixing.
Mention that while nominal typing adds safety, it can introduce complexity and boilerplate. In domains like aerospace, the extra safety is often worth it.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.