Skip to content

Type Inference in TypeScript

How TypeScript infers types automatically and the rules governing inference contexts.

What you'll learn

  • How TypeScript infers types from variable initializers
  • The difference between let and const inference (widening vs literal)
  • Contextual typing for function parameters and return values
  • Best common type inference for arrays and unions

Concept

Automatic Inference

TypeScript infers types when you do not annotate them. let count = 0 infers number, let name = "Alice" infers string, and let items = [1, 2, 3] infers number[]. Inference follows the initializer's type, so you rarely need to annotate local variables. The golden rule is: annotate function signatures (parameters have no inference context) and public APIs, but let inference handle local variables.

let vs const and Widening

let x = "hello" infers string (the widened type), because let variables can be reassigned to any string. const x = "hello" infers the literal type "hello", because const cannot be reassigned. This distinction matters for union types and literal types. You can force widening with an explicit annotation (const x: string = "hello") or force a literal with as const (let x = "hello" as const).

Contextual Typing

TypeScript infers types from context, not just initializers. When you write ["a", "b"].map(s => s.length), TypeScript knows s is string because map is called on a string[]. This contextual typing flows from the expected type into expressions, so you rarely need to annotate callback parameters. It also works with object literals, function returns, and generic type arguments — the compiler uses the surrounding context to fill in missing types.

Example

              // let widens to the general type; const keeps the literal.
let count = 42;        // inferred: number (widened)
const limit = 42;      // inferred: 42 (literal type)
let name = "Alice";    // inferred: string (widened)
const greeting = "Hi"; // inferred: "Hi" (literal type)

// 'as const' forces literal types for arrays and objects.
const config = { port: 3000, host: "localhost" } as const;
// type: { readonly port: 3000; readonly host: "localhost" }

// Contextual typing — s is inferred as string from the array.
const lengths = ["apple", "banana", "cherry"].map(s => s.length);
// lengths is inferred as number[]

// Best common type — TypeScript finds the common supertype.
const mixed = [1, 2, "three"]; // inferred: (string | number)[]

// Function return type is inferred from the body.
function double(x: number) {
  return x * 2; // return type inferred as number
}

console.log(count, limit, name, greeting, config.port, lengths, mixed);
            

TypeScript infers types from initializers and context. let widens to general types; const keeps literals. as const forces literal/readonly types. Contextual typing infers callback parameter types from their usage.

Try it

  • JSON to TypeScript

    Generated types show where inference would work vs where annotations help.

  • Code Diff

    Compare explicitly annotated code with its inferred equivalent.

Common mistakes

The mistake

Annotating every variable instead of relying on inference

The fix

Let TypeScript infer types for local variables. Only annotate function parameters, return types on public APIs, and places where the inferred type is too broad or unclear.

The mistake

Expecting let to preserve literal types like const does

The fix

let widens to the general type (string, number) because the variable can be reassigned. Use const for literal types, or use 'as const' to force them on let or object properties.

Practice Type Inference in TypeScript

7 exercises

Practice Type Inference in TypeScript

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors