Type Safety and Type Soundness
You'll learn the precise meaning of type safety and soundness—the core guarantees a good type system provides.
What is Type Safety?
Type safety means that a program is free from type errors—situations where operations are applied to values of incompatible types. A type-safe language prevents you from treating a string as an integer, accessing non-existent fields, or calling methods that don't exist.
Think of type safety as a contract: if the compiler accepts your program, you won't encounter type-related runtime crashes. Languages like Java and Haskell aim for strong type safety, while C and assembly provide weaker guarantees.
What is Type Soundness?
Type soundness (or "soundness" for short) is the formal property that guarantees this safety. A type system is sound if:
Well-typed programs don't go wrong.
More precisely: if a program passes type checking, it will never reach an undefined or stuck state due to type errors during execution. The types accurately predict runtime behavior.
The Analogy
Imagine a quality control system at a factory:
- Type safety is the goal: no defective products reach customers
- Type soundness is the guarantee: the inspection process is rigorous enough that it actually achieves that goal
A sound type system means the compiler's promises are trustworthy. An unsound system might accept programs that later crash with type errors.
Why It Matters
Soundness gives you confidence. In a sound type system, once your code compiles, whole categories of bugs simply cannot occur. You can reason about program behavior just by looking at types.
Key Takeaway: Type safety is freedom from type errors; type soundness is the formal guarantee that well-typed programs won't encounter type errors at runtime—the compiler's checks are sufficient to prevent type-related failures.