Join our Newsletter — 33% off our NHI Course
Home› Glossary› Cyber Security› Union Type
Cyber Security

Union Type

← Back to Glossary
By NHI Mgmt Group Updated October 30, 2026 Domain: Cyber Security

A union type lets a variable or parameter accept one of several defined types. In TypeScript, it is useful when a function must handle different concrete entities through one code path without creating a misleading shared base interface. The compiler narrows the type as needed, but runtime logic may still need explicit type context.

What a union type is doing in TypeScript

A union type lets one value be one of several declared shapes, so code can model real variation without pretending every case shares the same structure. That matters when a function must accept different concrete entities through one path, while the compiler still enforces which operations are safe after narrowing.

Why union types are useful

Union types are most valuable when a domain has legitimate alternatives and no single shared base interface would be accurate. They keep the type system honest by saying “one of these forms” instead of collapsing distinct cases into an artificial common parent that hides important differences.

That design improves expressiveness and can reduce accidental misuse. Instead of relying on loosely typed values or broad abstractions, the program can represent each allowed option explicitly and let the compiler block invalid assumptions before runtime.

Narrowing and runtime handling

A union type usually needs narrowing before code can safely use members that exist only on one branch of the union. In TypeScript, that may happen through property checks, discriminant fields, instanceof checks, or other control-flow analysis that reduces the possible set of types.

The key point is that the type system does not remove the need for runtime logic. If the program receives different concrete values on the same code path, it still has to inspect the value and choose the correct branch, especially when the union includes objects with different fields or behaviours.

When union types are a better fit than inheritance

Union types are often the better choice when the alternatives are genuinely distinct rather than variants of one stable abstraction. A misleading shared base interface can obscure the fact that each branch has different required data, different invariants, or different handling rules.

Used well, a union type makes those differences explicit to both the compiler and the reader. That can produce clearer API contracts, safer refactoring, and less ambiguity than a hierarchy that exists mainly to satisfy a type signature.

Free weekly newsletter

Subscribe to the NHI & AI Identity Journal

The latest on NHI and Agentic AI security – articles, research, breaches, news and events every week.

Bonus 33% off our NHI Course when you subscribe.

NHIMG Editorial Note
Reviewed and updated by the NHIMG editorial team on October 30, 2026.
NHI Mgmt Group — the #1 independent authority on Non-Human Identity, IAM, and Agentic AI security. nhimg.org