Type Inference
- In Turkish
- Tür çıkarımı
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
// 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
- Data TypeProgramming Fundamentals, p. 14A data type is a classification that tells a program what kind of value a piece of data holds, such as a number or text, and which operations work on it.
- GenericsProgramming Fundamentals, p. 24Generics are a language feature that lets you write functions, classes, and types that work with many data types while still keeping full type safety.
- TypeScriptWeb Development, p. 50TypeScript is a programming language built on JavaScript that adds static types, catching many bugs before the code runs, and compiles to plain JavaScript.
- CompilerProgramming Fundamentals, p. 11A compiler is a program that translates source code written in a programming language into a lower-level form, such as machine code, that a computer can run.
- VariableProgramming Fundamentals, p. 56A variable is a named storage location in a program that holds a value, such as a number or a piece of text, which the code can read and change as it runs.
- Static TypingProgramming Fundamentals, p. 53Static typing means the types of variables and expressions are checked before the program runs, usually by the compiler, so many type errors are caught early.
Spotted a mistake or something missing on this page?Suggest an edit