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.
A type system classifies the values in a program, such as integers, text strings, lists or user-defined records, and specifies which operations make sense for each. Adding two numbers is valid; adding a number to a list may not be. By attaching types to variables, function parameters and return values, the type system lets a language implementation detect certain mistakes, either before the program runs or at the moment the faulty operation is attempted. Types also document intent, telling readers and tools what kind of data a piece of code expects.
Type systems vary along several dimensions. Static typing checks types before execution, as in Java, C, Rust and Haskell, while dynamic typing checks them as the program runs, as in Python and Ruby. Strong and weak typing describe, somewhat loosely, how freely a language converts between types: JavaScript will silently turn a number into a string in some expressions, while Python raises an error instead. Languages also differ in whether types must be written out or can be inferred, an approach associated with the Hindley-Milner type system used in the ML family of languages.
Type systems are grounded in mathematical logic, and a deep correspondence between types and logical propositions has made them central to research on proving programs correct. In everyday practice, richer types catch bugs early, power editor features such as autocompletion and make large codebases easier to change safely. Gradual typing lets dynamically typed languages add optional types, as TypeScript does for JavaScript and type hints do for Python. The trade-off between type safety and flexibility shows up as extra annotation effort and occasional rejection of programs that would have run correctly.
Key points
- A type system assigns types to values and expressions and rules out operations that do not fit them.
- Static type checking happens before a program runs, while dynamic checking happens during execution.
- Type inference, associated with the Hindley-Milner system, lets compilers deduce many types automatically.
- Gradual typing, as in TypeScript and Python type hints, adds optional static checks to dynamic languages.
Related entries
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.
Type Safety
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.
TypeScript
Programming language developed by Microsoft that adds optional static types to JavaScript and compiles to plain JavaScript for any runtime.
Compiler
Program that translates source code written in one programming language, usually a high-level one, into another form such as machine code.
Static Analysis
Examination of source code or binaries without executing them, using automated tools to find bugs, security flaws and quality issues.
Functional Programming
Programming paradigm that builds software by composing pure functions, treating computation as evaluating expressions rather than changing state.
Rust
Systems programming language that guarantees memory safety without a garbage collector through its ownership model, first stable in 2015.
Python
High-level, general-purpose programming language created by Guido van Rossum, known for readable syntax and widely used in data science and AI.
Featured in guides
Referenced by
Other entries that discuss or point to Type System.