What is Type Inference in TypeScript?
Learn how TypeScript's type inference works, including contextual typing, const vs let narrowing, and best-common-type inference, with clear code examples.
Expected Interview Answer
Type inference is TypeScript's ability to automatically determine a variable's, parameter's, or return value's type from context, without you writing an explicit type annotation.
When you write `const age = 25`, TypeScript infers `age: number` from the literal value, so you rarely annotate every variable manually. Inference also flows through function return types, generic type parameters, and array/object literals via a process called contextual typing. Inference happens entirely at compile time; the inferred type is checked and then erased before the code runs as plain JavaScript. Best-common-type inference widens a union like `[1, 'a']` to `(number | string)[]`, and `const` declarations narrow literal types more tightly than `let` because the value cannot be reassigned.
- Reduces boilerplate by removing redundant type annotations
- Still catches type errors even without explicit annotations
- Contextual typing infers parameter types from expected function signatures
- `const` yields narrower, more precise literal types than `let`
- Works seamlessly with generics to infer type parameters from arguments
AI Mentor Explanation
Type inference is like an umpire reading a batter's grip and stance to determine they're right-handed without the batter announcing it out loud. The umpire still applies the same LBW and boundary rules regardless, having silently deduced the relevant fact from context rather than requiring an explicit declaration before the ball is bowled.
Step-by-Step Explanation
Step 1
Literal assignment
`const age = 25` infers `number` from the literal value with no annotation required.
Step 2
Contextual typing
When a function is assigned to a typed variable or passed as a callback, TypeScript infers parameter types from the expected signature.
Step 3
Best common type
For an array literal like `[1, 'two', 3]`, TypeScript infers the union `(number | string)[]` by finding a type covering all elements.
Step 4
Return type inference
A function's return type is inferred from its `return` statements unless you explicitly annotate one.
Step 5
const narrowing
`const x = 'GET'` infers the literal type `'GET'`, while `let x = 'GET'` widens to `string` since it could be reassigned.
What Interviewer Expects
- Can explain inference works without needing explicit annotations everywhere
- Knows the difference in inferred type between `const` and `let`
- Understands contextual typing infers callback parameter types
- Can describe best-common-type inference for array/object literals
- Knows inference happens at compile time only, with zero runtime cost
Common Mistakes
- Over-annotating every variable, adding noise inference already handles
- Assuming `let` preserves the same narrow literal type as `const`
- Not realizing inferred return types can silently widen when refactored
- Believing inference happens at runtime rather than compile time
Best Answer (HR Friendly)
“Type inference means TypeScript can figure out the type of a value automatically just by looking at how it's used, so you don't have to label every single variable by hand. This keeps code cleaner while TypeScript still catches type mistakes for you.”
Code Example
const method = 'GET'; // inferred as literal type 'GET'
let verb = 'GET'; // inferred as widened type string
function request(m: 'GET' | 'POST') {}
request(method); // OK, 'GET' matches the literal union
// request(verb); // Error: string is not assignable to 'GET' | 'POST'Follow-up Questions
- How does contextual typing infer parameter types in callbacks?
- Why does `let` widen a string literal but `const` does not?
- What is best-common-type inference for mixed array literals?
- Can you force TypeScript to infer a narrower type using `as const`?
- How does inference interact with generic function calls?
MCQ Practice
1. What type does TypeScript infer for `const status = 'active'`?
Because `const` cannot be reassigned, TypeScript infers the narrow literal type 'active' rather than widening to string.
2. What type does TypeScript infer for `let status = 'active'`?
Since `let` allows reassignment, TypeScript widens the inferred type to the general `string` type.
3. When does type inference happen in the TypeScript compilation process?
Inference is a compile-time analysis; once types are checked, they are erased and have no effect on the runtime JavaScript.
Flash Cards
What is type inference? — TypeScript automatically determining a type from context, without an explicit annotation.
How does `const` affect inferred type versus `let`? — `const` infers a narrow literal type; `let` widens to the general type since it may be reassigned.
What is contextual typing? — Inferring a callback's parameter types from the expected function signature it's assigned or passed to.
When does inference occur? — Entirely at compile time — inferred types are checked and then erased before runtime.