Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 4 of 9
Type Inference - When TypeScript Knows Best
TypeScript's type inference engine is powerful - it tracks types through assignments, function returns, array operations, and even control flow. Understanding where inference works and where it needs help is the key to writing clean, annotation-minimal TypeScript.
Inference - Variables, Functions & Control Flow
// ── Variable inference ────────────────────────────────────────
let x = 10; // inferred: number
x = "hello"; // ❌ Error: Type 'string' is not assignable to type 'number'
x = 20; // ✅ fine
// ── Array inference ────────────────────────────────────────────
const tags = ["design", "backend", "api"]; // string[]
const mixed = [1, "two", true]; // (number | string | boolean)[]
// ── Function return type inference ────────────────────────────
function double(n: number) {
return n * 2; // TypeScript infers return type: number
}
function greet(name: string) {
return "Hello, " + name; // inferred: string
}
// ── Object inference ───────────────────────────────────────────
const task = {
id: 1,
title: "Launch feature",
done: false,
};
// inferred: { id: number; title: string; done: boolean }
// ── Control flow narrowing (inference + narrowing) ─────────────
function processInput(input: string | number) {
if (typeof input === "string") {
// TypeScript narrows: input is string here
console.log(input.toUpperCase()); // ✅
} else {
// TypeScript narrows: input is number here
console.log(input.toFixed(2)); // ✅
}
}
// ── When inference FAILS and you need to annotate ─────────────
// 1. Empty array - TypeScript infers never[] without help
const emails: string[] = []; // ✅ annotate empty arrays
// 2. Function parameters - TypeScript cannot infer these
function createUser(name: string, age: number) { /* ... */ }
// 3. Variables initialised to null/undefined
let currentUser: { name: string } | null = null;
// 4. When you want a wider type than inference gives
const status: string = "active"; // prevents narrowing to literal "active"Visual architecture and concepts for Type Inference - When TypeScript Knows Best
Common Mistakes
- Fighting inference by annotating everything - this creates maintenance burden when types change (annotations and values both need updating).
- Expecting inference on function parameters - TypeScript cannot infer params. They must always be annotated.
- Ignoring widened inference on `let` - `let x = 'hello'` infers `string` (not `'hello'`). Use `const x = 'hello'` or `as const` to keep the literal type.
Key Takeaways
- TypeScript's type inference engine is powerful - it tracks types through assignments, function returns, array operations, and even control flow.
- Fighting inference by annotating everything - this creates maintenance burden when types change (annotations and values both need updating).
- Expecting inference on function parameters - TypeScript cannot infer params. They must always be annotated.
- Ignoring widened inference on `let` - `let x = 'hello'` infers `string` (not `'hello'`). Use `const x = 'hello'` or `as const` to keep the literal type.
Finished this lesson?