Lesson 6 / 28

Type Inference

Let TypeScript infer types for local variables and learn where explicit annotations are still worth writing.

TS infers for you

No annotation needed — the initial value tells TS the type, and it's remembered from then on.

let count = 5;          // inferred as number
count = "five";
// Error: Type 'string' is not assignable to type 'number'.

When to still annotate

Let inference handle local variables. Annotate function parameters, return types on public APIs, and empty arrays/objects — inference has nothing to go on there.