type system

term

programming

Kept in this browser · My desk

The rules of a programming language that classify values into types and refuse operations that do not fit them, either before the program runs (static) or while it runs (dynamic).

C, Pascal, Java, Go, Rust and TypeScript check types before running; Python, JavaScript, Ruby and Lua check them while running. The ML family added type inference in the 1970s; Python's Python Enhancement Proposal (PEP) 484 (2014) and Elixir 1.20 (June 2026) brought gradual typing to dynamic languages.

What it is. A type system is the set of rules by which a programming language classifies values into types (integer, string, list of integers, a function from one type to another) and refuses operations that do not fit them. The Rust reference puts the point concretely: "Every variable, item, and value in a Rust program has a type", and the type defines "the interpretation of the memory holding it and the operations that may be performed on the value". Without types, the same bits could be read as a number, a pointer or text.

Three questions. When: statically, by the compiler, or dynamically, by the runtime; Python Enhancement Proposal (PEP) 484 states that from Python's type hints "no type checking happens at runtime" and that Python will remain dynamically typed, the hints being read by separate checkers such as mypy, which describes itself as "a static type checker for Python". How types are compared: by name (nominal, as in Java) or by members (structural: "type compatibility in TypeScript is based on structural subtyping"). How much is written: ML's type inference lets a compiler deduce that a function filtering, squaring and summing a list has type int list -> int without a single annotation, and Elixir 1.20 infers and gradually checks types in code that has none.

Static and strong are different words. Luca Cardelli and Peter Wegner's survey On Understanding Types, Data Abstraction, and Polymorphism (Computing Surveys of the Association for Computing Machinery, ACM, December 1985) separated them: languages "in which the type of every expression can be determined by static program analysis are said to be statically typed", while languages "in which all expressions are type-consistent are called strongly typed", and if a language is strongly typed "its compiler can guarantee that the programs it accepts will execute without type errors". A language can be strongly typed with some checks at run time. The same paper classified polymorphism, refining Christopher Strachey: universal polymorphism, parametric (one function for every type, like a list's length) or inclusion (subtypes and inheritance), against ad hoc polymorphism, overloading and coercion.

The guarantee. Robin Milner's A Theory of Type Polymorphism in Programming (Journal of Computer and System Sciences, 1978) gave the field its slogan. He defined a compile-time type-checking algorithm, W, for a language with polymorphic procedures, and proved a semantic soundness theorem that "well-typed programs cannot 'go wrong'" and a syntactic one that what the algorithm accepts is well typed; a checker based on W was already working in ML for the Edinburgh LCF (Logic for Computable Functions) prover. Every claim that a type system is "sound" means that theorem holds for it.

In practice. Milner wrote that the polymorphic type discipline had "been incorporated in the LCF metalanguage ML" and in use "for nearly 2 years", and that its compile-time checker had proved "quite simple": a procedure gets a polymorphic type in general, and a monomorphic one only when its argument and result types are fixed by context. The TypeScript handbook shows the structural side with four lines: an interface Pet with a name, a class Dog with a name, and the assignment pet = new Dog(), which is "OK, because of structural typing"; "In nominally-typed languages like C# or Java, the equivalent code would be an error" because Dog never declares that it implements Pet. Neither answer is wrong. Nominal typing catches accidental matches; structural typing fits code built from anonymous objects, as JavaScript is.

How strictly values are converted. Conversion is a separate matter from checking: "5" * "2" is 10 in JavaScript and Perl and an error in Python. Implicit coercions make programs shorter and errors quieter.

Types as a security mechanism. Type rules matter for security in both directions. Weak coercions let a validator and a consumer disagree about the same input. A broken type assumption inside an engine or a C program is type confusion, Common Weakness Enumeration (CWE)-843, a staple of browser exploits. And types can enforce safety on code from strangers: the Java Virtual Machine verifies every class file "by type checking" before running it, so that downloaded bytecode cannot treat an integer as a pointer.

What goes wrong. Type annotations that are never checked at run time (PEP 484's hints are ignored by the interpreter), escape hatches such as casts, any and unsafe blocks, and implicit conversions are where type systems stop protecting. Gradual systems help only as far as code is annotated or inferred. Unsound corners exist in widely used languages, which is why their checkers document them.

Neighbours. The type system is one of the four parts of a programming language, the basis of ML-style inference and of TypeScript's gradual checking, the information a just-in-time (JIT) compiler gathers at run time, and the thing memory safety and type confusion are about. Brazilian practice says sistema de tipos, tipagem estática and tipagem dinâmica.

Also known as: static typing, dynamic typing, type checking, type inference, gradual typing, sistema de tipos, tipagem estática, tipagem dinâmica

Sources

← Back to T Back to the glossary