Course contentsShow
← Computer Science
Lesson 2016 of 2,87245. Type Systems and SemanticsFree lesson

Type Inference in Modern Languages

How languages like Rust, TypeScript, and Kotlin implement practical type inference.

Type Inference in Modern Languages

You'll learn how modern languages like Rust, TypeScript, and Kotlin balance explicitness and inference in their practical type systems.

Why Modern Languages Differ

While academic languages like ML and Haskell lean heavily on pure Hindley-Milner inference, production languages must balance developer experience, readability, and tooling support. Languages like Rust, TypeScript, and Kotlin implement selective or local type inference rather than full global inference.

Rust's Approach

Rust requires explicit type annotations for function signatures but infers types within function bodies. This design makes APIs crystal-clear while reducing local boilerplate:

fn calculate(x: i32, y: i32) -> i32 {  // explicit signature
    let sum = x + y;     // inferred: i32
    let doubled = sum * 2;  // inferred: i32
    doubled
}

The compiler uses bidirectional type checking to flow information from both context and usage.

TypeScript's Gradual Inference

TypeScript infers types where possible but allows optional annotations. It uses control flow analysis to refine types through your code:

function process(input: string | number) {
    let result = input + 10;  // inferred based on operations
    return result;
}

Kotlin's Smart Casts

Kotlin infers variable types from initializers and uses nullability checks to automatically narrow types within scopes—combining inference with runtime safety patterns.

The Common Thread

All three prioritize predictability over maximum inference. They require enough annotations (especially at API boundaries) to make code self-documenting and IDE-friendly, while inferring types in local, obvious contexts where unification is straightforward.

Key Takeaway: Modern languages implement practical, constrained type inference—favoring explicit function signatures with local inference inside bodies, balancing terseness with code clarity and tooling needs.