Type Inference
Type inference is TypeScript working out the type of a value from code you already wrote, so you do not have to state it. Most types in a well-written TypeScript file are inferred rather than annotated.
Detailed Explanation
Every value in TypeScript has a type. That does not mean you have to type every value out by hand. The compiler reads the initializer, the surrounding expression, and the signature it is being passed into, and it fills in the blanks. This is inference, and it is the reason TypeScript feels like JavaScript instead of like Java.
The simplest form reads the right-hand side of a declaration. const port = 8080 gives port the type number without a single extra character.
Inference behaves differently for const and let, and this trips up almost everyone once. A let binding can be reassigned, so the compiler widens the type to something reassignment-friendly: let method = "GET" has type string. A const binding can never be reassigned, so the compiler keeps the narrow answer: const method = "GET" has the literal type "GET". Widening also applies inside object literals, because their properties are mutable — { method: "GET" } infers { method: string }. as const switches widening off when you need the literal to survive.
For arrays, TypeScript computes the best common type: it looks at every element and produces the union that covers them all. [1, 2, 3] is number[]. [1, "two"] is (string | number)[]. It does not pick a single element's type and force the rest to match.
The most useful form is contextual typing, which runs in the opposite direction. Instead of reading a value to find its type, the compiler reads the expected type of the position and pushes it inward. When you write ids.map(n => n * 2), nobody annotated n. The compiler knows ids is number[], knows map expects a callback whose first parameter matches the element type, and hands n the type number. The same mechanism types e as MouseEvent in a click handler and types the fields of an object literal you pass to a function.
Inference has a hard limit, and it is worth naming: a standalone function parameter has no context to infer from. The compiler cannot see the call sites while it checks the body, so there is nothing to read. Such a parameter becomes an implicit any, which is what the noImplicitAny flag reports. Parameters of callbacks are fine, because the surrounding signature supplies the context. Parameters of your own declared functions are not.
Return types are inferred from whatever the body returns. That is convenient and slightly risky. An inferred return type on an exported function is a public contract you never wrote down, so an internal edit can silently change it and the error appears at the call site instead of in the file you touched.
The working rule that falls out of all this: annotate the boundaries, infer the insides. Inference costs nothing at runtime, and it costs a little compile time, but the real cost is readability when a type is inferred through five layers and no human can tell what it became without hovering over it.
Code Examples
1. const keeps the literal type, let widens it.
// 1. const keeps the literal type, let widens it.
const method = "GET"; // type is "GET"
let fallback = "GET"; // type is string — because you could reassign it
function request(verb: "GET" | "POST") {}
request(method); // fine
request(fallback);
// Error: Argument of type 'string' is not assignable to parameter of type '"GET" | "POST"'.
// Object literal properties widen too, because they are mutable.
const config = { method: "GET" }; // type is { method: string }, not { method: "GET" }2. Contextual typing: the expected type flows inward, so callbacks need no annotations.
// 2. Contextual typing: the expected type flows inward, so callbacks need no annotations.
const cartCents = [1299, 450, 8999];
const total = cartCents.reduce((sum, cents) => sum + cents, 0);
// sum and cents are both inferred as number; total is number
const labels = cartCents.map((cents) => `$${(cents / 100).toFixed(2)}`);
// labels is string[] — inferred from what the callback returns3. Best common type for arrays, and where inference gives up.
// 3. Best common type for arrays, and where inference gives up.
const mixed = [1, "two", 3]; // type is (string | number)[]
function slugify(input) { // no initializer, no context to read
return input.toLowerCase();
}
// Error (with noImplicitAny): Parameter 'input' implicitly has an 'any' type.
function slugifyFixed(input: string) { // annotate the boundary...
return input.toLowerCase(); // ...and the return type infers as string
}Common Gotchas
Widening silently discards literal types
`let status = "active"` is `string`, so it will not satisfy a parameter typed `"active" | "archived"`. Use `const`, `as const`, or an explicit annotation when the exact literal is the point.
Unannotated parameters are the one place inference cannot help
the compiler checks a function body without knowing who calls it. Leaving parameters bare under `noImplicitAny` is an error, and leaving them bare without that flag is an untyped hole that spreads.
Inferred return types on exports are an accidental API
nothing pins them, so refactoring the body changes the signature. Annotate return types on exported functions so the break is reported where you made it.
Inference reads the code, not your intent
if a value is initialized to `null` or `[]`, the compiler infers `null` or `never[]` and later assignments fail. Annotate the declaration in those cases rather than fighting the inferred type with assertions.