Glossary termProgramming language concept

Static Typing

Also known as: Static type checking, Statically typed language

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.

In a statically typed language, every variable and expression has a type that is known and checked before the program is executed, typically during compilation. If code tries to pass text where a number is required, or calls a method that does not exist on a given type, the compiler reports an error and refuses to produce a program. C, Java, Go, Rust, Haskell and TypeScript are statically typed. In dynamically typed languages such as Python and Ruby, the same mistakes surface only when the offending line actually runs.

Static typing does not always mean writing types everywhere. Many languages infer types from context, so a variable assigned a number can be understood as an integer without an annotation; ML, Haskell and Rust rely heavily on inference, while Go and later versions of Java and C# can infer the types of local variables. Type checkers for dynamic languages, such as mypy for Python and the TypeScript compiler for JavaScript, bring static analysis to code that would otherwise be checked only at run time. Static types also help compilers generate faster code, because the layout of data is known in advance.

The debate between static and dynamic typing is long-running. Advocates of static typing point to earlier error detection, safer large-scale refactoring, better editor tooling and types that serve as checked documentation. Advocates of dynamic typing value faster prototyping, less ceremony and flexibility for data whose shape is not known in advance. Empirical studies of which approach yields fewer defects have produced mixed results. In practice the gap has narrowed, as static languages gained inference and dynamic ones adopted optional type annotations checked by external tools.

Key points

  • Static typing checks types before execution, catching many mismatches at compile time rather than in production.
  • Type inference lets statically typed languages omit many explicit annotations.
  • Tools such as mypy and TypeScript add static checking on top of dynamically typed languages.
  • Evidence on whether static typing reduces defects is mixed, and the choice often depends on project size and goals.

Related entries

Glossary term

Type System

Programming

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.

Glossary term

Type Safety

Programming

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.

Technology

TypeScript

Programming

Programming language developed by Microsoft that adds optional static types to JavaScript and compiles to plain JavaScript for any runtime.

Glossary term

Compiler

Programming

Program that translates source code written in one programming language, usually a high-level one, into another form such as machine code.

Glossary term

Static Analysis

ProgrammingCybersecurity

Examination of source code or binaries without executing them, using automated tools to find bugs, security flaws and quality issues.

Technology

Rust

ProgrammingFOSS

Systems programming language that guarantees memory safety without a garbage collector through its ownership model, first stable in 2015.

Technology

Java

Programming

Class-based, object-oriented programming language from Sun Microsystems whose programs run on the Java Virtual Machine across many platforms.

Glossary term

Refactoring

Programming

Process of restructuring existing code to improve its design, readability or maintainability without changing its external behavior.