TypeScript Type Inference

🧠 TypeScript Type Inference (Beginner → Advanced)

Type Inference in TypeScript means the compiler can automatically determine the type of a variable, function, or expression without you explicitly writing it.
This is one of TypeScript’s biggest strengths—less code, more safety.


1️⃣ What Is Type Inference?

Type inference is TypeScript’s ability to figure out types from values and usage.

let x = 10;
// inferred as: number

✔ No need to write : number
✔ Still type-safe


2️⃣ Variable Type Inference ⭐

let count = 5;
// count: number
let name = “Sanjit”;
// name: string

let active = true;
// active: boolean

❌ Reassigning wrong type:

count = "five"; // Error

3️⃣ let vs const Inference ⭐ (Very Important)

let

let status = "loading";
// status: string

const

const status = "loading";
// status: "loading" (literal type)

const infers narrower types


4️⃣ Function Return Type Inference ⭐

function add(a: number, b: number) {
return a + b;
}
// inferred return type: number

You can still be explicit:

function add(a: number, b: number): number {
return a + b;
}

✔ Both are valid


5️⃣ Function Parameter Inference 🔍

const multiply = (a: number, b: number) => a * b;

📌 Parameters usually need explicit types
📌 Return types are inferred


6️⃣ Array Type Inference ⭐

let nums = [1, 2, 3];
// number[]
let mix = [1, “a”];
// (number | string)[]

✔ Union types inferred automatically


7️⃣ Object Type Inference ⭐

let user = {
id: 1,
name: "Rahul",
active: true
};

Inferred as:

{
id: number;
name: string;
active: boolean;
}

⚠️ Cannot add new properties:

user.email = "x@y.com"; // Error

8️⃣ Contextual Typing ⭐⭐

Type inferred from context.

window.addEventListener("click", (e) => {
e.preventDefault(); // e inferred as MouseEvent
});

✔ Common in callbacks & event handlers


9️⃣ Best Common Type Inference ⭐⭐

let values = [0, 1, null];
// (number | null)[]

TypeScript finds the best common type


🔟 Type Inference with Generics 🔥

function identity<T>(value: T): T {
return value;
}
const a = identity(10); // T = number
const b = identity(“hello”); // T = string

✔ Generic type inferred automatically


1️⃣1️⃣ When Inference Is NOT Enough ⚠️

any Inference (Bad)

let x;
// x: any ❌

Fix:

let x: number;

1️⃣2️⃣ Type Inference vs Explicit Types ⭐

SituationRecommended
Simple variablesInference
Function returnInference
Public APIsExplicit
Complex objectsExplicit
any inferenceAvoid

1️⃣3️⃣ Common Mistakes ❌

  • Relying on any

  • Overusing explicit types

  • Forgetting const for literals

  • Assuming parameter types are inferred

  • Ignoring inference errors


📌 Interview Questions (Must Prepare)

  1. What is type inference?

  2. Difference between let and const inference

  3. When should you use explicit types?

  4. What is contextual typing?

  5. How does inference work with arrays?

  6. Can TypeScript infer generic types?


✅ Summary

✔ TypeScript infers types automatically
✔ Reduces boilerplate code
const creates literal types
✔ Inference works for variables, returns, arrays, objects
✔ Explicit types needed for APIs & clarity
✔ Core concept for clean & safe TypeScript

You may also like...