Type inference
Вывод типов
A compiler or type checker can work out the type of a variable or expression from the surrounding code without asking you to write it explicitly. For example, an initial value of `42` can tell the checker that a variable holds a number.
What type inference does
Type inference lets you leave out a type annotation when the checker can work out the type from the code. In TypeScript, let retries = 3 gives retries the type number. Assigning "none" to it later produces a type error. The type is still checked before the program runs; the only thing missing is the annotation. That's why inferred typing is not the same as dynamic typing.
Where the checker gets its information
An initializer is one clue, but it isn't the only one. A checker may infer a function's return type from its return expressions or work out a generic type argument from the values passed to a function. The expected type can also guide the checker in the other direction: if a function expects a callback that takes a string, that expectation can supply the callback parameter's type. TypeScript calls this contextual typing.
Each language sets its own rules about which clues the checker may use. For example, Java's var supports inference for local declarations such as var name = "Ada", but it does not let you omit a method's return type. And when the available clues aren't enough, the checker may need an annotation or an explicit type argument.
Example: inferring a generic type argument
function wrap<T>(value: T): { value: T } {
return { value };
}
const scores = wrap([10, 20]);
const labels = scores.value.map(score => score.toFixed(1));
In the call to wrap, the argument has type number[], so the checker infers T as number[]. Writing wrap<number[]>([10, 20]) would be redundant here. On the next line, the type of scores.value tells the checker that map passes a number to the callback. toFixed returns a string, so labels has type string[]. Notice that the checker gets the type of score from the signature of map, not from seeing toFixed in the callback body.
When to write the type anyway
Let inference handle obvious local types, but consider annotating a public function's return type. Without an annotation, a refactor can change the inferred return type and break callers. With an explicit return type, the checker flags an incompatible return value at the function itself. An annotation also helps when the code doesn't show your intent, as in const names: string[] = [].
Neither inference nor annotations validate incoming data. A TypeScript type on a variable doesn't prove that a server actually sent JSON with the expected fields. When the data isn't trusted, validate it at runtime.
How it comes up in interviews
You might be asked to predict the type of an expression and identify the evidence the checker uses. A common TypeScript example is let mode = "dark" versus const mode = "dark" as standalone declarations. The first is inferred as string so it can hold another string later; the second keeps the literal type "dark". Explain why the types differ rather than just naming them.