Skip to main content

Type Inference

Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/type-inference

In short

Type inference is a compiler feature that works out the type of a variable or expression automatically, so you don't have to write every type annotation.

What is type inference?

Type inference means the compiler or type checker figures out types for you by looking at how values are created and used. If you write let count = 5 in TypeScript, it knows count is a number without an explicit annotation, and it reports an error if you later assign a string to it. The code is still statically typed; you simply write fewer types by hand.

The compiler infers types from several clues: the value a variable starts with, the return statements of a function, the arguments passed to a generic function, and the context an expression appears in, such as a callback passed to map. TypeScript, Kotlin, Swift, Rust, Go, and Scala infer the types of local variables, Java and C# offer var, C++ has auto, and functional languages such as Haskell and OCaml can infer the types of almost an entire program.

It works much like a reader who figures out the meaning of an unfamiliar word from the sentence around it. Inference keeps code short and readable, but many teams still write explicit types on function parameters, public return types, and exported APIs, because those annotations document intent and produce clearer error messages.

Type inference is often confused with dynamic typing. In a dynamically typed language such as JavaScript or Python, types are checked only while the program runs, and a variable can hold a number now and a string later. With inference, types are fixed and checked before the program runs; the compiler just fills them in for you, and when it has no clues, as with an unannotated function parameter, TypeScript falls back to any, which the noImplicitAny option reports as an error.

Key takeaways

  • Type inference lets the compiler determine types without explicit annotations.
  • Inferred code is still statically typed and checked before it runs.
  • Types are inferred from initial values, return statements, generic arguments, and context.
  • Explicit annotations remain useful on function parameters and public APIs.
  • Inference is not dynamic typing: an inferred type can't change later.

Example

Inferred types in TypeScripttypescript
// No annotations, but every type is known at compile time
let count = 5;                   // inferred as number
const names = ["Ada", "Linus"];  // inferred as string[]

function double(x: number) {
  return x * 2;                  // return type inferred as number
}

// name is inferred as string, lengths as number[]
const lengths = names.map((name) => name.length);

count = "five"; // Error: Type 'string' is not assignable to type 'number'.

Readers ask

Is type inference the same as dynamic typing?

No. With dynamic typing, types are checked while the program runs and a variable can change type. With type inference, the compiler determines fixed types before the program runs, so type errors are still caught early.

Should I rely on type inference or write types explicitly?

A common practice is to let inference handle local variables and simple expressions, and to write explicit types for function parameters, public return types, and exported APIs. This keeps code concise while documenting the important boundaries.

Does Python have type inference?

Python itself is dynamically typed, but static type checkers such as mypy infer types from your code and type hints. For example, they know that after x = 5, x is an int.

See also

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings