Hint
TypeScript deduces types automatically from context — assignments, return values, default parameters
Type inference is TypeScript's ability to automatically determine a value's type without an explicit annotation.
Where inference applies:
const x = 42 infers numberfunction greet(name = 'World') infers name: string// Variable initialization
const count = 0; // inferred: number
const name = 'Alice'; // inferred: string
const active = true; // inferred: boolean
// Function return
function add(a: number, b: number) {
return a + b; // return type inferred as number
}
// Contextual typing — callback param inferred from array type
const nums = [1, 2, 3];
nums.forEach(n => console.log(n.toFixed(2))); // n inferred as number
// Generic inference
function identity<T>(value: T): T { return value; }
const result = identity('hello'); // T inferred as string
const x = 42 not const x: number = 42. Annotate function parameters and public API shapes where inference can't help consumers.