Type Inference
Subject: Computer and Technology noun /ˈtaɪp ˌɪn.fər.əns/ 1 definition
- programming-languages
- type-systems
- compiler-design
- static-analysis
-
Definition (noun)
Entry #89 · UpdatedType Inference is the automated process by which a compiler, interpreter, or static analyzer deduces the data types of expressions, variables, parameters, and return values from surrounding program context without requiring explicit type annotations in source code. Unlike dynamic typing—which defers type checks until runtime—or manifest static typing—which demands manual annotations on declarations—type inference preserves compile-time type safety while minimizing syntactic verbosity. By analyzing literals, operator signatures, control flow, and structural constraints, it reconstructs missing type information and detects type mismatches before execution.
Common Forms
Local Type Inference: Deduces a variable or expression's type directly from its initializer or immediate syntactic context without solving global constraints across functions, as seen with
autoin C++,varin Java and C#, and:=in Go.Hindley-Milner (Global) Type Inference: Reconstructs the most general polymorphic types across entire functions and modules through constraint generation, unification, and let-polymorphism without any annotations, forming the foundation of Haskell, OCaml, and Standard ML.
Bidirectional Type Inference: Combines bottom-up type synthesis from subexpressions with top-down type checking against expected signatures, supporting inference for rich features such as closures, generics, and subtyping in languages like Rust, Scala, and TypeScript.
Flow-Sensitive Type Inference: Narrows and refines inferred variable types along specific execution paths based on conditional checks, type guards, and null assertions, as used in TypeScript, Kotlin, and Python static type checkers.
Example
In TypeScript, writing
const prices = [12.5, 19.0, 7.25]; const total = prices.reduce((sum, p) => sum + p, 0);lets the compiler inferpricesasnumber[], the callback parameterssumandpasnumber, andtotalasnumberwithout explicit annotations. If a later line callstotal.toUpperCase(), the type checker immediately reports a compile-time error becausetoUpperCasedoes not exist on the inferrednumbertype.