Function Parameter Context
What you'll learn: How TypeScript automatically figures out parameter types in callbacks, saving you from typing them explicitly.
The Core Idea
When you pass a function as a callback (like to .map() or .forEach()), TypeScript already knows what types the parameters should be based on where you're using that function. This is called contextual typing for function parameters.
Because TypeScript knows the expected function signature from context, you don't need to write type annotations on the callback's parameters — they're inherited automatically.
How It Works in Practice
When you call numbers.forEach(...), TypeScript knows forEach expects a callback that receives a number (and index, and array). So when you write the arrow function inside, TypeScript infers that num is a number:
const numbers = [1, 2, 3];
// TypeScript infers 'num' is number
numbers.forEach(num => {
console.log(num.toFixed(2)); // Works! num is a number
});
You didn't write num: number, but TypeScript figured it out from the array type and the forEach signature.
The Inheritance Chain
Think of it like a form with pre-filled fields. The function signature is the template, and when you write a callback that fits that template, TypeScript fills in the parameter types for you automatically.
Without this context, you'd need to annotate every callback parameter manually, which would be tedious and verbose.
Key Takeaway: TypeScript infers callback parameter types from the function signature that expects them, letting you omit explicit type annotations while keeping full type safety.