Type Inference

What

Type inference is TypeScript’s ability to automatically determine the type of a variable based on the value assigned to it, without requiring an explicit annotation.

How

Instead of writing out the type explicitly, you simply initialize the variable. TypeScript “looks” at the value and assigns the corresponding type:

// Explicit (unnecessary)
const log: string = "starting server...";

// Inferred (preferred)
const log = "starting server...";

Why

Type inference reduces boilerplate and makes your code cleaner and easier to read. It allows you to maintain full type safety without having to manually annotate every single variable, which leaves less room for human error during manual updates.

Summary

Let TypeScript infer types whenever the value makes the type obvious; only use explicit annotations when the compiler needs more information.