Type systems

Hand-drawn clusters of nested rounded terracotta rectangles over sand, umber and moss hatching.

A type system is a set of rules that assigns types to the expressions of a program, and type theory studies such rules. A static checker applies them before the program runs, and a soundness proof establishes which errors the rules exclude (Cardelli, 1997; Krishnamurthi & Politz, n.d.). In logic, certain type theories represent a proposition as a type and a proof as an expression of that type (Wadler, 2007).

Static checkers can flag some bugs that reached real projects, and experiments find that the effect on development time varies by task (Gao et al., 2017b; Khan et al., 2022; Mayer et al., 2012). Researchers dispute how far static typing is associated with fewer defects across GitHub projects (Ray et al., 2014; Berger et al., 2019b; Ray et al., 2019).

From paradoxes to typed expressions

Types began as a remedy for logical contradictions. Bertrand Russell used them in his 1908 theory to address paradoxes, including that of a greatest ordinal. His ramified theory added extra orders within types, which later work removed on the way to simple type theory (Russell, 1908; Kamareddine et al., 2002).

In 1940, Alonzo Church formulated simple type theory using features of the lambda calculus, a notation for defining and applying functions. Church credited Leon Chwistek and F. P. Ramsey with suggesting a simpler form of Russell’s system (Church, 1940).

What a checker guarantees

A type checker guarantees only what its rules and their proof cover. As an illustration, a rule may require that a function’s argument has the function’s declared input type. Static type checking applies such rules before the program runs; a dynamic check tests a requirement during execution (Cardelli, 1997; Krishnamurthi & Politz, n.d.; Ray et al., 2014).

A sound type system guarantees that accepted programs avoid a specified class of errors. In one approach to proving soundness, each step of running a program preserves its type, and the proof excludes states that would get stuck on a forbidden error. The model determines which failures count and which runtime errors it still permits (Cardelli, 1997; Wright & Felleisen, 1994; Krishnamurthi & Politz, n.d.).

Languages differ in how far they carry such guarantees. For a model of a realistic subset of Rust, RustBelt gives a machine-checked safety proof. By contrast, TypeScript deliberately accepts some uses that can violate its static types. The term “strong typing” is ambiguous: one usage means a sound static type system, another includes detecting type confusion during execution (Jung et al., 2018; Microsoft TypeScript Team, n.d.c; Wright & Felleisen, 1994; Ray et al., 2014).

Reusing code and inferring types

Polymorphism lets an operation work with values of different types. With parametric polymorphism, one implementation is described in terms of a type parameter: an identity function returns its argument unchanged, whether a number or a string (Cardelli & Wegner, 1985). With overloading, one name selects different implementations, as addition does for integers and real numbers. Subtyping permits a value of a more specific type where a broader type is expected (Cardelli & Wegner, 1985; Milner, 1978).

Type inference derives types from code and its uses. The Hindley–Milner tradition asks when a checker can find a principal type, a most general type from which the other valid types follow. Damas and Milner showed this for the part of ML their model covers (Damas & Milner, 1982). In some systems that allow type parameters in more places than ML, complete inference is impossible, and programmers supply annotations to guide it (Peyton Jones et al., 2007).

Static typing in practice

Studies of static typing measure different things: bugs a checker would have flagged, time spent on tasks, defects across projects, or the cost of runtime checks.

Bugs a checker can flag

A checker run on old code would have flagged a minority of the bugs that reached real projects. In a JavaScript study, Flow 0.30, another JavaScript type checker, and TypeScript 2.0 each flagged 15% of the sampled public bugs. The researchers used the eventual fixes to guide where they added annotations, which declare expected types (Gao et al., 2017b).

A study of 210 Python projects estimated that the mypy checker could have flagged 15% of the defects that needed a correction, or 11% of all sampled defects. It often flagged mishandling of null values, which mark the absence of a value. Many defects fixed by changing values or adding or removing code escaped detection (Khan et al., 2022).

What a checker detects depends on the annotations it is given. In a sample of 40 known Python type-related bugs, three checkers together detected 14 before researchers added annotations and 29 afterward (Xu et al., 2023).

Development and maintenance time Weak evidence: few or small studies support this

The effect on development time depends on the task, in a few small experiments. In a 49-person experiment, participants spent about 27 hours each building a parser, and typing made no overall difference. With an undocumented application programming interface (the operations one program offers another), each typing approach sped up some tasks (Hanenberg, 2010; Mayer et al., 2012).

A 33-person experiment found that static typing helped participants understand undocumented code and fix type errors, but not fix errors in what the code does (Hanenberg et al., 2014). A reanalysis found the advantage for identifying unfamiliar classes significant only among participants who started with dynamic typing (Siegmund et al., 2020).

Defects across projects

Researchers disagree on how much of a reported link between static typing and defects survives reanalysis. A 2014 study of 729 GitHub projects across 17 languages reported a modest advantage for static over dynamic typing among functional languages. It estimated larger effects for project size, team size and the size of code changes (Ray et al., 2014).

After correcting the analysis, a reanalysis found four language associations that met its statistical test, each with a very small estimated effect. Revising the language groupings changed the result for two of the five categories (Berger et al., 2019b). The original authors replied that the reanalysis largely reproduced their results, and agreed that effects were small and could be absent. Their 2017 journal version, on 728 projects, kept the direction of the findings; the reanalysis authors said the data they received was the conference version’s (Ray et al., 2019; Ray et al., 2017; Berger et al., 2019a).

Other repository studies use other measures of bugs. A comparison of JavaScript and TypeScript projects found no evidence of a lower share of bug-fix commits or faster bug closure in the TypeScript projects. A study of 600 projects associated static typing with shorter times to resolve bugs, and with fixes that touched more files (Bogner & Merkel, 2022; Zhang et al., 2020a).

Runtime cost of mixing typing styles

Gradual typing lets programmers choose which parts of a program to check statically, and extend checking as they add type information. Typed Scheme showed how a multi-module program can run while only some modules have types (Siek & Taha, 2006; Siek et al., 2015; Tobin-Hochstadt & Felleisen, 2008). In sound gradual typing, the language enforces its static guarantees where checked and unchecked parts interact, and the cost of those checks depends on how they interact (Siek et al., 2015; Greenman et al., 2019).

A mix of checked and unchecked parts can run much more slowly than either endpoint. In a Typed Racket benchmark, the slowest mix of one program took 114.47 times as long as its untyped version, while the fully typed version took 0.99 times as long. To capture this spread, a benchmark can test each mix at a chosen level, such as modules, or sample mixes when there are too many to test (Takikawa et al., 2016; Greenman et al., 2019).

Implementation changes can reduce this overhead. A system that compiles code as it runs removed more than 90% of it on typical benchmarks from earlier Typed Racket work. For Reticulated Python, the standard interpreter took up to six times as long as for equivalent untyped programs. Combining an optimization with PyPy, another Python implementation, left no measured overhead (Bauman et al., 2017; Vitousek et al., 2019).

Propositions as types

The Curry–Howard correspondence relates propositions to types and proofs to typed expressions; simplifying a proof corresponds to evaluating an expression. Martin-Löf’s account of constructive proof, for example, treats a proof of “if A, then B” as a function that turns a proof of A into a proof of B (Wadler, 2007; Martin-Löf, 1984).

The correspondence depends on how proofs are recorded. In one formulation, proofs and terms pair one to one; with extra rules governing the use of assumptions, distinct proofs may share an expression. Girard and Reynolds independently developed System F, which can express functions that take types as parameters; in its logic, a statement can be made about all propositions (Murfet & Troiani, 2026; Wadler, 2007).

Proof assistants

A proof assistant checks a mathematical argument by checking the type of the expression that encodes it. In early Coq, commands called tactics built a proof expression, and the checker then checked the whole expression. Its Calculus of Constructions had been influenced by System F and by Automath, a system for checking mathematical material (Coquand et al., 2015; Coquand & Huet, 1988; Geuvers & Nederpelt, 2022).

Coq checked a formal proof of the four-colour theorem. Lean began in 2013 as an interactive theorem prover based on the Calculus of Inductive Constructions, the extension of the calculus that Coq implemented (Gonthier, n.d.; Coquand et al., 2015; de Moura & Ullrich, 2021).

What else types can express

Dependent types let a type refer to a value: in Agda, the length of a vector can be part of its type, so a function’s input type can distinguish vectors of different lengths (Agda developers, n.d.).

Linear types specify how values are used. In Linear Haskell, if code consumes the result of a linear function exactly once, the function consumes its argument exactly once (Bernardy et al., 2018).

Homotopy type theory connects type theory with homotopy theory, the study of spaces up to continuous deformation. Its univalence axiom allows structures with the same form, in a precise mathematical sense, to be identified (The Univalent Foundations Program, 2013).

Further reading

  • Cardelli defines type soundness relative to the errors a language forbids (Cardelli, 1997).
  • Wadler develops the correspondence between propositions, types and proofs across different formal systems (Wadler, 2007).
  • Gao and colleagues estimate detectable bugs by adding types to historical versions of JavaScript programs (Gao et al., 2017b).
  • Berger and colleagues show how rerunning and correcting the GitHub analysis changed its results (Berger et al., 2019b).
  • Greenman and colleagues describe how to measure costs across mixes of typed and untyped code (Greenman et al., 2019).

Known gaps

What this article cannot tell you yet.

  1. The debate over type theory and set theory as foundations for mathematics remains outside the scope of this article.
  2. Coverage of homotopy type theory focuses on univalence. It leaves open how practitioners use the framework in formal mathematics.
  3. The controlled experiments on development time cover specific programming tasks. Long-term effects across teams and projects are outside their scope.
  4. The historical detection rates apply to sampled defects, checker versions and annotation procedures. They leave open how many defects teams would prevent in ordinary development.

References

Solid source
Rated highly trustworthy by the research, and every passage taken from it was found in it.
Weak source
Rated low, or a passage taken from it was not found in it.
Medium trust
Rated medium by the research.
Not checked
Rated high, but its passages could not all be checked against it.
Show all 44 references