Type Safety
Also known as: Type soundness
Degree to which a programming language prevents type errors, in which a value is used as if it were a different, incompatible kind of data.
Type safety is the guarantee that a program cannot misuse data by treating a value of one type as if it were another, incompatible type. In a type-safe language, an attempt to treat an integer as a memory address, or a string as a function, is either rejected before the program runs, as with static type checking, or stopped with a well-defined error when it happens. What is never allowed is undefined behavior, where the program silently reads or corrupts memory as though the operation were valid. Type safety is closely related to memory safety, and the two are often discussed together.
Formally, a type system is called sound if well-typed programs cannot reach such invalid states, a property usually proved by showing that each step of execution preserves types and that well-typed programs never get stuck. Robin Milner's 1978 work on type polymorphism for the ML language produced an influential early result of this kind. Languages differ widely in practice. Java, C#, Haskell and safe Rust are designed to be type-safe; C and C++ allow casts and pointer arithmetic that can break type safety; and dynamic languages such as Python enforce it at run time by raising errors.
Type safety matters for reliability and security. Many classic exploits, from buffer overflows to type confusion bugs in browser engines, rely on a program interpreting memory as the wrong kind of data, so type-safe and memory-safe languages remove large classes of vulnerabilities by construction. Even safe languages offer escape hatches for low-level work, such as unsafe blocks in Rust or unchecked casts, and the guarantee then depends on that code being correct. Stronger guarantees usually come at the cost of stricter rules for programmers.
Key points
- A type-safe language prevents values from being used as incompatible types, avoiding undefined behavior.
- Errors may be caught at compile time or reported at run time, but are never silently allowed to corrupt data.
- C and C++ permit operations that break type safety, while Java, C# and safe Rust are designed to prevent them.
- Type confusion and memory corruption bugs are common security flaws that type-safe languages largely rule out.
Related entries
Type System
Set of rules a programming language uses to assign types such as integer or string to values and to check that operations use them consistently.
Static Typing
Approach in which a program's types are checked before it runs, usually by a compiler, so that many type errors are caught ahead of execution.
Rust
Systems programming language that guarantees memory safety without a garbage collector through its ownership model, first stable in 2015.
Pointer
Variable that stores the memory address of another value, letting programs refer to data indirectly and build linked structures.
Vulnerability
A weakness in software, hardware, configuration or process that an attacker could exploit to compromise confidentiality, integrity or availability.
C
General-purpose programming language created by Dennis Ritchie at Bell Labs in the early 1970s, foundational for operating systems and embedded software.
Garbage Collection
Automatic memory management in which a language runtime finds and reclaims memory occupied by objects a program can no longer use.
TypeScript
Programming language developed by Microsoft that adds optional static types to JavaScript and compiles to plain JavaScript for any runtime.
Referenced by
Other entries that discuss or point to Type Safety.