BeCuriouzLearn something new every day
Type Annotations & Inference: Let the Compiler Do the Work
💡 Annotate where TypeScript has nothing to learn from — mainly function parameters — and let inference handle the rest.
In the last card you saw TypeScript's basic types. Now the key question: do you have to write those types everywhere? Almost never. A type annotation is when you declare the type with a colon — let count: number = 5. Type inference is when TypeScript figures out the type on its own from the value you assign.
let a = 5; // inferred: number
let b = "hello"; // inferred: string
let c = [1, 2, 3]; // inferred: number[]Here a: number would be redundant — TS already knows. The rule of thumb: let inference handle it whenever a value is present, and only annotate when there's no value to learn from. The classic case is a function's parameters, which arrive empty and must be annotated. Return types, by contrast, are usually inferred from what you return:
function double(n: number) { // param annotated (required)
return n * 2; // return type inferred: number
}Why prefer inference? It's less code to read, and it stays correct when you refactor — change the value and the type updates automatically. Annotations shine as a deliberate contract: on function parameters, on a variable declared without an initial value (let id: string;), or when you want to catch a mismatch. If you write let total: number = "5", TS errors immediately — the annotation is a guardrail, not decoration.