What is Type Inference in TypeScript?
Learn how TypeScript type inference deduces types from your values, cutting boilerplate while keeping code fully type-safe. With examples and MCQs.
Expected Interview Answer
Type inference is TypeScript's ability to automatically deduce the type of a value from its context, so you get static type checking without writing explicit type annotations everywhere.
When you initialize a variable, return a value, or pass an argument, the compiler picks the most appropriate type from the surrounding code. This happens for variable initialization, default parameters, return types, and generic type arguments. Inference keeps code concise while still catching type errors, and you add explicit annotations only where the inferred type is too wide or too narrow.
- Less boilerplate — fewer explicit annotations to write and maintain
- Still fully type-safe at compile time
- Cleaner, more readable code
- Refactoring stays consistent as inferred types update automatically
- Better editor autocomplete and hover hints
AI Mentor Explanation
A seasoned umpire watches the ball pitch and the batter's stance and instantly knows it is an lbw appeal without anyone spelling out every rule aloud. TypeScript's compiler works the same way: it observes how you initialise a value and infers its type automatically, so you rarely have to declare it in words.
Step-by-Step Explanation
Step 1
Write a plain assignment
Assign a literal like let count = 5 with no type annotation.
Step 2
Compiler reads the value
TypeScript sees the number literal 5 and infers the type as number.
Step 3
Type is now fixed
Reassigning count to a string is flagged as an error even though you never wrote 'number'.
Step 4
Inference for return types
A function returning a + b where both are numbers is inferred to return number.
Step 5
Annotate only when needed
Add explicit types when the inferred one is too wide (e.g. any) or you want a narrower contract.
What Interviewer Expects
- A clear definition of inference vs explicit annotation
- Awareness that inference happens on initialization
- Knowledge that return types are inferred
- Understanding of when to annotate manually
- Mention of contextual typing
Common Mistakes
- Claiming TypeScript has no types because you did not write them
- Annotating everything and losing inference's benefits
- Confusing inference with type assertion (as)
- Assuming inference always picks the narrowest type
Best Answer (HR Friendly)
“Type inference means TypeScript is smart enough to figure out what kind of data a variable holds just by looking at the value you give it. So you write less code, but you still get warned early if you accidentally mix up your data types.”
Code Example
// No annotations, yet fully typed
let count = 5; // inferred: number
let name = 'Ada'; // inferred: string
// Return type inferred as number
function add(a: number, b: number) {
return a + b;
}
count = 'six'; // Error: Type 'string' is not assignable to type 'number'
// Contextual typing infers the parameter type
const nums = [1, 2, 3];
nums.forEach((n) => {
// n is inferred as number here
console.log(n.toFixed(2));
});Follow-up Questions
- What is the difference between inference and type assertion?
- What is contextual typing in TypeScript?
- When does TypeScript infer the 'any' type?
- How does 'const' vs 'let' affect literal type inference?
- What is the 'best common type' algorithm?
MCQ Practice
1. Given 'let x = 10;', what type does TypeScript infer for x?
With 'let', the literal 10 is widened to its general type, number, so x is inferred as number.
2. Where does type inference NOT automatically apply?
An uninitialised variable has no value to infer from, so TypeScript falls back to 'any' (or errors under strict settings).
3. What does 'const greeting = "hi";' infer as the type?
const bindings cannot be reassigned, so TypeScript infers the narrow literal type "hi" instead of widening to string.
Flash Cards
What is type inference? — TypeScript automatically deducing a value's type from context instead of an explicit annotation.
Does inference reduce type safety? — No — inferred types are checked just as strictly as explicit ones.
let vs const inference — let widens literals (5 -> number); const keeps the literal type (5).
What is contextual typing? — Inference driven by the expected type of the surrounding expression, e.g. callback parameters.